array_replace()
Similar to array_merge(), this function combines two or more arrays into one.
The main differences with this function are:
- Any duplicate key will be overwritten (not just string keys)
- Nothing is re-indexed
It goes through the arrays listed - in order - and any existing keys have their values overwritten. Any new keys are appended to the end of your new array.
$arr1 = array( 'a' => 'apple', 'b' => 'banana', 'c' => 'carrot' ); $arr2 = array( 'b' => 'ball', 2 => 'stick', 17 => 'monkey' ); $arr3 = array( 'a' => 'zebra', 'b' => 'yak' ); $new_a = array_replace( $arr1, $arr2, $arr3 ); /* array ( 'a' => 'zebra', 'b' => 'yak', 'c' => 'carrot', 2 => 'stick', 17 => 'monkey' ) */ // order is important $new_b = array_replace( $arr1, $arr3, $arr2 ); /* array ( 'a' => 'zebra', 'b' => 'ball', 'c' => 'carrot', 2 => 'stick', 17 => 'monkey' ) */
See the manual entry for array_replace()
