note 121100 deleted from function.array by cmb

From: Date: Fri, 19 May 2017 09:28:55 +0000
Subject: note 121100 deleted from function.array by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-209067@lists.php.net to get a copy of this message
Note Submitter: steve at websmithery dot co dot uk ---- It is worth noting that, where possible, PHP silently converts string keys into integers, e.g. <?php $foo = [ '7' => 'This key is a string' ]; print_r($foo); ?> will output: Array ( [7] => This key is a string ) Note: the key has been recast as an integer. One consequence of this is that an array cannot have equal integer and string keys: <?php $foo = [ '7' => 'This key is a string', 7 => 'This key is an integer' ]; print_r($foo); ?> will output: Array ( [7] => This key is an integer ) PHP recasts the first, string key as an integer and so the second (integer) key overwrites it. This may seem to be of little consequence as equal string and integer keys are, perhaps, unlikely, but one unexpected result of this recasting is when the array_merge() function is used. When the arrays are merged, string keys are unaffected, but integer keys are renumbered sequentially in the resulting array. This means that if an array being merged has string keys, one of which just happens to be recastable as an integer, then that particular key will change.

« previous php.notes (#209067) next »