Jump to content


This topic is now archived and is closed to further replies.


Array_merge_recursive() On The Same Array

Recommended Posts

Very simple question which either I am smoking crack and therefore cannot see the answer...or is just impossible to do. Using PHP5...

$array =
[0] => Array ( [Misc] => Blah )
[1] => Array ( [Animals] => Moo )
[2] => Array ( [Animals] => Oink )
[3] => Array ( [Stuff] => Thing1 )
[4] => Array ( [Stuff] => Thing2 )

All I need to accomplish is to....

$result = array_merge_recursive($array['0'],$array['1'],$array['2'],$array['3'],$array['4']);

Which yields...

$result =
[Misc] => Blah
[Animals] => Array ( [0] => Moo [1] => Oink )
[Stuff] => Array ( [0] => Thing1 [1] => Thing2 )

no problem there...however I do NOT know how many arrays I will need to list within the array_merge_recursive() function...so I tried to do a count and create a variable with the array broken into the proper format with the "," between them like...

$variable = $array['0'],$array['1'],$array['2'],$array['3'],$array['4'];

however array_merge_recursive($variable) refuses to work...

soooooo...the million dollar question is....

how can I either load up a variable properly formatted which the array_merge_recursive function will accept?


how can I do an array_merge_recursive($array['0'],$array['1],etc,etc,etc, until EOF??)

Any takers??

Share this post

Link to post
Share on other sites
[code]$result = array();

foreach ($array as $ar) {
    foreach ($ar as $k=>$val)
    $result[$k][] = $val;

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.