Array_keys vs foreach when getting all keys

When getting all the keys of an array that has the best performance? array_keys or foreach? Interestingly, if array_keys is a function that uses a foreach loop or a loop to get the keys .. (since foreach is a language construct), therefore foreach is better.

But I'm not sure if array_keys uses a foreach loop to get keys

So which one is better -

foreach ($value as $key => $value) {
    $pkey = ':' . $key;
    $placeholders[$pkey] = $value;
}
$value = array_keys($placeholders);

or

$keys = array();
foreach ($value as $key => $value) {
    $pkey = ':' . $key;
    $placeholders[$pkey] = $value;
    $keys[] = $pkey;
}
+4
source share
5 answers

, , , : - .
, , . , , .

( PHP 5.6) 10'000 100 000 , - 0,025 .

script1:

<?php

    $arr = range(1, 100000);
    $keys = array_keys($arr);

?>

2:

<?php

    $arr = range(1, 100000);

    foreach($arr as $k => $v)
        $keys[] = $k;

?>

, .

, , foreach , "" , , foreach array_keys():

, @EliasVanOotegem, , , ! >

script1:

number of ops:  8
compiled vars:  !0 = $arr, !1 = $keys
line     # *  op                           fetch          ext  return  operands
---------------------------------------------------------------------------------
   3     0  >   SEND_VAL                                                 1
         1      SEND_VAL                                                 100000
         2      DO_FCALL                                      2  $0      'range'
         3      ASSIGN                                                   !0, $0
   5     4      SEND_VAR                                                 !0
         5      DO_FCALL                                      1  $2      'array_keys'
         6      ASSIGN                                                   !1, $2
         7    > RETURN                                                   1

2:

number of ops:  14
compiled vars:  !0 = $arr, !1 = $k, !2 = $v, !3 = $keys
line     # *  op                           fetch          ext  return  operands
---------------------------------------------------------------------------------
   3     0  >   SEND_VAL                                                 1
         1      SEND_VAL                                                 100000
         2      DO_FCALL                                      2  $0      'range'
         3      ASSIGN                                                   !0, $0
   5     4    > FE_RESET                                         $2      !0, ->12
         5  > > FE_FETCH                                         $3      $2, ->12
         6  >   OP_DATA                                          ~5      
         7      ASSIGN                                                   !2, $3
         8      ASSIGN                                                   !1, ~5
   6     9      ASSIGN_DIM                                               !3
        10      OP_DATA                                                  !1, $8
        11    > JMP                                                      ->5
        12  >   SWITCH_FREE                                              $2
        13    > RETURN                                                   1

, array_keys , foreach

array_keys() foreach for, . :

/* {{{ proto array array_keys(array input [, mixed search_value[, bool strict]])
   Return just the keys from the input array, optionally only for the specified search_value */
PHP_FUNCTION(array_keys)
{
     zval *input,                /* Input array */
         *search_value = NULL,  /* Value to search for */
        **entry,               /* An entry in the input array */
           res,                 /* Result of comparison */
          *new_val;             /* New value */
    int    add_key;             /* Flag to indicate whether a key should be added */
    zend_bool strict = 0;       /* do strict comparison */
    HashPosition pos;
    int (*is_equal_func)(zval *, zval *, zval * TSRMLS_DC) = is_equal_function;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|zb", &input, &search_value, &strict) == FAILURE) {
        return;
    }

    if (strict) {
        is_equal_func = is_identical_function;
    }

    /* Initialize return array */
    if (search_value != NULL) {
        array_init(return_value);
    } else {
        array_init_size(return_value, zend_hash_num_elements(Z_ARRVAL_P(input)));
    }
    add_key = 1;

    /* Go through input array and add keys to the return array */
    zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(input), &pos);
    while (zend_hash_get_current_data_ex(Z_ARRVAL_P(input), (void **)&entry, &pos) == SUCCESS) {
        if (search_value != NULL) {
            is_equal_func(&res, search_value, *entry TSRMLS_CC);
            add_key = zval_is_true(&res);
        }

        if (add_key) {
            MAKE_STD_ZVAL(new_val);
            zend_hash_get_current_key_zval_ex(Z_ARRVAL_P(input), new_val, &pos);
            zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &new_val, sizeof(zval *), NULL);
        }

        zend_hash_move_forward_ex(Z_ARRVAL_P(input), &pos);
    }
}
/* }}} */

:

+9

( , , array_keys)

?

foreach ($value as $key => $value) {
    $placeholders[$key] = $value;            // Useless loop
}
$keys = array_keys($placeholders);

$keys = array_keys($value);   // if $value is your original array.

, . . PHP

$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));

foreach array_keys , , . , array_keys ().

+3

5x10'000'000 :

$value = array('a'=>'1','b'=>'2','c'=>'3');

first version 22.61sec

second version 20.15sec

+2

, :

  • -
  • , , , ,
  • array_keys foreach , array_keys , foreach
  • PHP - -, : , . , C, , PHP array_keys, .

TL;

, foreach ( ), , . , PHP ( OP-)

+1

, , , foreach, array_key :

null, 'ZP9098' = > null); foreach (array_keys ($ mykeys) $key) {   echo gettype ($ key). "\n"; } ? >
0

All Articles