note 100240 deleted from function.array-merge by cmb

From: Date: Mon, 19 Nov 2018 17:04:01 +0000
Subject: note 100240 deleted from function.array-merge by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-209730@lists.php.net to get a copy of this message
Note Submitter: nzujev [AT] gmail [DOT] com ---- Be ready to surprise like this one. array_merge renumbers numeric keys even if key is as string. keys '1' & '2' will be updated to 0 & 1, but '1.1' & '1.2' remain the same, but they are numeric too (is_numeric('1.1') -> true) It's better to use '+' operator or to have your own implementation for array_merge. <?php $x1 = array ( '1' => 'Value 1', '1.1' => 'Value 1.1', ); $x2 = array ( '2' => 'Value 2', '2.1' => 'Value 2.1', ); $x3 = array_merge( $x1, $x2 ); echo '<pre>NOT as expected: '. print_r( $x3, true ) .'</pre>'; $x3 = $x1 + $x2; echo '<pre>As expected: '. print_r( $x3, true ) .'</pre>'; ?> NOT as expected: Array ( [0] => Value 1 [1.1] => Value 1.1 [1] => Value 2 [2.1] => Value 2.1 ) As expected: Array ( [1] => Value 1 [1.1] => Value 1.1 [2] => Value 2 [2.1] => Value 2.1 )

« previous php.notes (#209730) next »