note 106037 deleted from function.array-merge by salathe
| From: | salathe@php.net | Date: | Mon, 06 Sep 2021 22:46:58 +0000 |
| Subject: | note 106037 deleted from function.array-merge by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-216906@lists.php.net to get a copy of this message | ||
Note Submitter: w_barath at hotmail dot com
----
I keep seeing posts for people looking for a function to replace numeric keys.
No function is required for this, it is default behavior if the + operator:
<?php
$a=array(1=>"one", "two"=>2);
$b=array(1=>"two", "two"=>1, 3=>"three",
"four"=>4);
print_r($a+$b);
?>
Array
(
[1] => one
[two] => 2
[3] => three
[four] => 4
)
How this works:
The + operator only adds unique keys to the resulting array. By making the replacements the first
argument, they naturally always replace the keys from the second argument, numeric or not! =)