RE: [PHP] merge 3 arrays in PHP3??
| From: | René Fournier | Date: | Wed, 17 Oct 2001 20:55:36 +0000 |
| Subject: | RE: [PHP] merge 3 arrays in PHP3?? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-71406@lists.php.net to get a copy of this message | ||
Thanks for the help, but that function is giving me parse error. Here's
what I've got:
-------------------------
function rec(&$ar1,&$ar2){
foreach ($ar2 as $k=>$v){ <<<PARSE ERROR<<<
if (isset($ar1[$k] && is_array($ar1[$k]) && is_array($ar2[$k])){
rec($ar1[$k],$ar2[$k]);
}elseif (!isset($ar1[$k]){
$ar1[$k]=$v;
}
}
}
$ar1 = file ('one.db');
$ar2 = file ('two.db');
rec($ar1,$ar2);
$fcontents = $ar1;
rsort ($fcontents);
-------------------------
I'm pretty new to PHP (and programming in general), but... Do $k and $v
need to be defined somewhere (as keys/values??)? I'm guessing this function
is meant to append $ar2 to $ar1, such as:
BEFORE
--one.db--
big,red,apple
long,yellow,banana
--two.db--
little,green,kiwi
small,orange,mandarin
AFTER
--one.db--
big,red,apple
long,yellow,banana
little,green,kiwi
small,orange,mandarin
(That's what I'm shooting for.) Thanks again.
...Rene
---
Rene Fournier
renefournier@yahoo.com
> -----Original Message-----
> From: Andrey Hristov [mailto:ahristov@icygen.com]
> Sent: Wednesday, October 17, 2001 9:09 PM
> To: René Fournier
> Subject: Re: [PHP] merge 3 arrays in PHP3??
>
>
> function rec(&$ar1,&$ar2){
> foreach ($ar2 as $k=>$v){
> if (isset($ar1[$k] && is_array($ar1[$k]) && is_array($ar2[$k])){
> rec($ar1[$k],$ar2[$k]);
> }elseif (!isset($ar1[$k]){
> $ar1[$k]=$v;
> }
> }
> }
>
> I'm sure at 10% that will work, bu I think after
> little(estimated) hacking it
> will go.
>
>
> --
> Andrey Hristov
> Web Developer
> Icygen Corporation
> BUILDING SOLUTIONS
> http://www.icygen.com
>
>
> On Wednesday 17 October 2001 03:56 pm, you wrote:
> > I'd like to merge three arrays, then sort them, but I have to
> do it in PHP3
> > (so I can't use, much to my disappointment array_merge_recursive()). Is
> > there another [simple] way to do this??
> >
> > This is what I've got now:
> > ------------------------------------
> > $array[1] = file ('one.db');
> > $array[2] = file ('two.db');
> > $array[3] = file ('three.db');
> >
> > for ($a = "1"; $a < 4; $a++) {
> >
> > $contents = $array[$a];
> > rsort ($contents);
> > ------------------------------------
> > And this is what I'd LIKE to have (or something like it that works :-)
> > ------------------------------------
> > $array[1] = file ('one.db');
> > $array[2] = file ('two.db');
> > $array[3] = file ('three.db');
> >
> > $contents = array_merge_recursive($array[1], $array[2], $array[3]);
> > rsort ($contents);
> > ------------------------------------
> > Notice, I want to sort the ENTIRE three arrays as ONE array, not
> > subsorts... (Thanks!)
> >
> > ...Rene
> >
> > ---
> > Rene Fournier
> > renefournier@yahoo.com
>
>