Jump to content


Photo

Array_merge_recursive() On The Same Array


  • Please log in to reply
1 reply to this topic

#1 eviltwinkie

eviltwinkie
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 28 June 2006 - 11:47 PM

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?

OR

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

Any takers??









#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 29 June 2006 - 06:04 AM

$result = array();

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

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users