So you want to merge the arrays recursively... if only such an array_merge_recursive
function existed... Why don't you try this:
$a = array(
array('colour' => 'green'),
array('colour' => 'blue'),
array('width' => 123)
);
$result = array();
foreach($a as $arr)
{
$result = array_merge_recursive($result, $arr);
}
var_dump($result);
That worked pretty darn well for me, as you can see for yourself here, too
True, in the given example width
won't be an array, so you get:
array('colour' => array('green','blue'),'width' => 123);
If you need everything to be an array, then a dirty fix would be to use a cast:
foreach($result as $k => $v) $result[$k] = (array) $v;
Reassigning the $result
values a second time, only casting them as an array ensures that all values will, evidently, be arrays. An array that is cast to an array will remain unchanged, just like (int) 1
still evaluates to 1. A primitive value (strings, integers, doubles,...) will be wrapped int an array, but an object will be transformed into an array, so be careful. If objects are likely to occur in this array:
foreach($result as $k => $v) $result[$k] = is_array($v) ? $v : array($v);
is probably the safer bet. However, I chose not to go for this approach as I still find it pretty tedious and silly to wrap everything up into an array, containing only 1 value...
For those of you with a strange preference for unmaintainable code, the following one-liner is a condensed, but notice free & working example of the same code:
foreach($a as $arr) $result = array_merge_recursive(isset($result) ? $result : array(), $arr);
This is as a response to Stuart Wakefield who suggested a one-liner using call_user_func_array
, which is something I'll always oppose, for as long as I live and breathe, BTW...