RE: [PHP] array_merge on 2d-arrays
| From: | Ford, Mike [LSS] | Date: | Wed, 07 May 2003 10:16:05 +0000 |
| Subject: | RE: [PHP] array_merge on 2d-arrays | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-146643@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: David T-G [mailto:davidtg-php@justpickone.org]
> Sent: 07 May 2003 03:35
>
> Hi, all --
>
> Given the snippet of code
>
> $a1 = array ( 'aa' => array ( 'a' => "aye", 'b'
> => "bee",
> 'c' => "see" ) , 'bb' => array ( 'd' =>
> 'dee' ) ) ;
> $a2 = array ( 'aa' => array ( 'e' => "eee", 'f'
> => "efh" ) ) ;
> $m1 = array_merge($a1,$a2) ;
> $m2 = array ( 'aa' => array_merge ($a1[aa],$a2[aa]) , 'bb'
> => $a1[bb]) ;
>
> and a quick
>
> print "<pre>\n" ;
> print "a1 : \n" ; print_r($a1) ;
> print "a2 : \n" ; print_r($a2) ;
> print "m1 : \n" ; print_r($m1) ;
> print "m2 : \n" ; print_r($m2) ;
> print "</pre>\n" ;
>
> we can see that $m1 simply steps on the 'aa' array from $a1
> and that $m2
> gives me what I'd naturally want. Is there a recursive
> array_merge that
> will handle multi-dimensional arrays so that I don't have to write
> something like this from scratch?
Uh, well at the bottom of the manual page for array_merge()
(http://www.php.net/array-merge) it says "See also array_merge_recursive()"
-- seems like a clue to me.
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@lmu.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211