note 65121 added to function.array-merge-recursive

From: Date: Mon, 24 Apr 2006 19:31:22 +0000
Subject: note 65121 added to function.array-merge-recursive
Groups: php.notes 
Request: Send a blank email to php-notes+get-109596@lists.php.net to get a copy of this message
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. ---- Server IP: 216.194.113.175 Probable Submitter: 65.106.184.146 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.array-merge-recursive.php Edit -- http://master.php.net/note/edit/65121 Del: integrated -- http://master.php.net/note/delete/65121/integrated Del: useless -- http://master.php.net/note/delete/65121/useless Del: bad code -- http://master.php.net/note/delete/65121/bad+code Del: spam -- http://master.php.net/note/delete/65121/spam Del: non-english -- http://master.php.net/note/delete/65121/non-english Del: in docs -- http://master.php.net/note/delete/65121/in+docs Del: other reasons-- http://master.php.net/note/delete/65121 Reject -- http://master.php.net/note/reject/65121 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#109596) next »