php - How do I merge two associative arrays such that key collisions result in an array of both values? -
i have 2 or more array same key, want merge these arrays keep value. exp:
$arr1 = array("message" => "message 1"); $arr2 = array("message" => "message 2"); echo "<pre>"; print_r($arr3 = somefunction($arr2,$arr1)); echo "</pre>";
i want result this:
array ( [message] => array ( [0] => message 1 [1] => message 2 ) )
array_merge_recursive()
: merges elements of 1 or more arrays values of 1 appended end of previous one. returns resulting array.
example :
<?php $arr1 = array("message" => "message 1"); $arr2 = array("message" => "message 2"); echo "<pre>"; print_r($arr3 = array_merge_recursive($arr2,$arr1)); echo "</pre>"; ?>
output :
array ( [message] => array ( [0] => message 2 [1] => message 1 ) )
more info : http://php.net/manual/en/function.array-merge-recursive.php
Comments
Post a Comment