Filling an array of PHP: check the index first?

If I'm deep in the nest of loops, I wonder which one is more efficient:

if (!isset($array[$key])) $array[$key] = $val;

or

$array[$key] = $val;

The second form is much more desirable as far as the code is read. Actually, the names are longer, and the array is multidimensional. So the first form ends up pretty rough in my program.

But I wonder if the second form can be slower. Since the code is one of the most frequently executed functions in the program, I would like to use a faster form.

In general, this code will be executed many times with the same value of "$ key". Therefore, in most cases, $ array [$ key] will already be installed, and isset () will return FALSE.

To clarify for those who are afraid that I am processing non-identical code, as if it were identical: as for this part of the program, $ val is a constant. It is unknown before execution, but it is installed earlier in the program and does not change here. Thus, both forms give the same result. And this is the most convenient place to get $ val.

+5
source share
9 answers

For the array you really want: array_key_exists($key, $array)instead isset($array[$key]).

+10
source

isset () is very fast with regular variables, but you have an array here. The hash map algorithm for arrays is fast, but it still takes more time than doing nothing.

, , , , , . , : , "" , , "".

, . , - "".

+3

, , $array[$key] , ? , , , , isset , . , , .... .

, ! $val $array[$key], $val $array[$key], .

( , , $val $array[$key], , , .)

+2

, , , .

script ?

+1

, , .

$anArray[ 'level1' ][ 'level2' ][ 'level3' ] = ...

, 2 3.

$anArray[ 'level1' ][ 'level2' ]

, , .

, , , , , .

:

<?php

function create_array_path( $path, & $inArray )
{
    if ( ! is_array( $inArray ) )
    {
        throw new Exception( 'The second argument is not an array!' );
    }
    $traversed = array();
    $current = &$inArray;

    foreach( $path as $subpath )
    {
        $traversed[] = $subpath;
        if ( ! is_array( $current ) )
        {
            $current = array();
        }
        if ( ! array_key_exists( $subpath, $current ) )
        {
            $current[ $subpath ] = '';
        }
        $current = &$current[ $subpath ];
    }
}


$myArray = array();

create_array_path( array( 'level1', 'level2', 'level3' ), $myArray );

print_r( $myArray );

?>

:

    Array
    (
        [level1] => Array
            (
                [level2] => Array
                    (
                        [level3] => 
                    )

            )

    )
+1

isset() , . , .

0

, , ? isset() . , isset. , .

0

PHP,

$array[$key] = !isset($array[$key]) ? $val : $array[$key];

.

0

PHP, . , PHP, , , PHP3 php3/php3_hash.c,

_php3_hash_exists :

  • key hashed
  • ,

_php3_hash_add_or_update:

  • , ,
    • ,

Therefore, it would seem that the installation is faster because there is only one function call, and this process of hashing and bucket search will be performed only once.

0
source

All Articles