note 65121 deleted from function.array-merge-recursive by bjori

From: Date: Mon, 24 Apr 2006 19:55:29 +0000
Subject: note 65121 deleted from function.array-merge-recursive by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-109603@lists.php.net to get a copy of this message
Note Submitter: Vladimir Kornea ---- The documentation for array_merge_recursive() states that if the input arrays have string keys, the keys will be preserved. This is not true. Example 1: $key = 'one'; $x = array($key => array('two-one' => 'one-two-one')); $y = array($key => array('two-two' => 'one-two-two')); $z = array_merge_recursive($x, $y); echo "\$key's type is " . gettype($key) . "\n"; print_r($z); Output 1: $key's type is string Array ( [one] => Array ( [two-one] => one-two-one [two-two] => one-two-two ) ) So far so good, array_merge_recursive() is acting as expected. Example 2: $key = '1'; $a = array($key => array('21' => '121')); $b = array($key => array('22' => '122')); $c = array_merge_recursive($a, $b); echo "\$key's type is " . gettype($key) . "\n"; print_r($c); Output 2: $key's type is string Array ( [0] => Array ( [21] => 121 ) [1] => Array ( [22] => 122 ) ) Even though the keys in $a and $b are strings ('1' rather than 1), they are treated as integers. I consider this a bug. Be aware of it because it might suddenly break your code for no apparent reason.

« previous php.notes (#109603) next »