array_merge on 2d-arrays
| From: | David T-G | Date: | Wed, 07 May 2003 02:35:24 +0000 |
| Subject: | array_merge on 2d-arrays | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-146613@lists.php.net to get a copy of this message | ||
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?
I'm doing this for ini file parsing; I'll have a host-wide master,
usually a client's site-wide setup, and possibly a per-directory file for
a special case. If I write my ini file with sections, I'm in exactly
this boat -- so at the moment I'm not using sections!
Worse yet, however, is that I have an array of sizes
viewsizes = "array(640,1024,'Original')"
to which it would be nice to add more -- but which puts me in this same
situation.
Unfortunately, I can also see the challenge of figuring out when to wipe
out and when to merge. Perhaps one client will want to add '256' to his
viewsizes array while perhaps another would want to have only a single
choice available. Naturally I'd say to step on the array that I define
(letting client 1 define his entire array rather than add to it) but to
not step on any section arrays, but somehow that seems too easy ;-)
Any thoughts? Has anyone already invented this wheel?
P.S. to John :
I ended up writing my own ini-reading snippet because I wanted to handle
comments differently. Check out the basics at justpickone.org/testme.phps
if you're interested, and thanks again.
TIA & HAND
:-D
--
David T-G * There is too much animal courage in
(play) davidtg@justpickone.org * society and not sufficient moral courage.
(work) davidtgwork@justpickone.org -- Mary Baker Eddy, "Science and Health"
http://justpickone.org/davidtg/ Shpx gur
Pbzzhavpngvbaf Qrprapl Npg!