note 106037 modified in function.array-merge by danbrown

From: Date: Thu, 06 Oct 2011 16:20:34 +0000
Subject: note 106037 modified in function.array-merge by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-183670@lists.php.net to get a copy of this message
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! =) --was-- 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: $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! =) http://php.net/manual/en/function.array-merge.php

« previous php.notes (#183670) next »