Array merge won't work as he needs to add the numerical values
involved, not just merge the arrays - there are overlap on the text
values, and any overlap needs to result in a val1 + val2.

-James


On 10/16/07, Daevid Vincent <[EMAIL PROTECTED]> wrote:
> > -----Original Message-----
> > From: Ladislav Andel [mailto:[EMAIL PROTECTED]
> > Sent: Tuesday, October 16, 2007 1:05 PM
> > To: James Ausmus; PHP List
> > Subject: Re: [PHP] combining 2 arrays
> >
> > Thank you very much!
> > It's exactly the code I was looking for!
> >
> > Lada
> > PS: my previous ungly code is half long now :)
> >
> > James Ausmus wrote:
> > > On 10/16/07, Ladislav Andel <[EMAIL PROTECTED]> wrote:
> > >
> > >> Hi list!
> > >> I read data from 2 databases and for the purpose of displaying the
> > >> information at my web interface as one piece
> > >> I need to combine the data into one array first.. Here is
> > my problem:
> > >>
> > >> Firstly, it reads from first DB and get this array: (it's a sum of
> > >> server names in table)
> > >>
> > >> arrayDB1 = array(array('8', 'SER'),  array('5','Asterisk'))
> > >>
> > >> When finished then it starts reading from second DB
> > >> where I would get
> > >>
> > >> arrayDB2 = array(array('6', 'XIP'),  array('4','Asterisk'))
> > >>
> > >>
> > >> Is there any function where I would get
> > >> result = array(array('8', 'SER'),  array('9','Asterisk'),
> > array('6','XIP'))
> > >>
> > >
> > > If you have to have data manipulation when combining the
> > arrays (such
> > > as adding the two separate values for "Asterisk" as per
> > your example),
> > > then there isn't a pre-defined function to do what you want. I'd
> > > change the arrangement of your arrays a bit, so that they looked
> > > something like the following (very untested):
> > >
> > > $arrDB1 = array('Asterisk' => 5, 'SER' => 8);
> > > $arrDB2 = array('Asterisk' => 4, 'SER' => 6);
> > >
> > > function addArrays($arr1, $arr2)
> > > {
> > >   $res = array();
> > >
> > >   if (is_array($arr1) and is_array($arr2))
> > >   {
> > >     $keys = array_merge(array_keys($arr1), array_keys($arr2));
> > >     foreach ($keys as $key)
> > >     {
> > >       $res["$key"] = $arr1["$key"] + $arr2["$key"];  //May have to
> > > check isset of each $arrX["$key"] before addition, not sure
> > >     }
> > >   } else
> > >   {
> > >     //Do appropriate error handling here...
> > >   }
> > >   return $res;
> > > }
> > >
> > >
> > >
> > >
> > >> Probably, it would be best to add number of Asterisk while cycling
> > >> through data of second DB.
> > >> The problem could be when databases have thousands of rows
> > in each DB.
> > >>
> > >> I'm just asking in case you know a better approach.
> > >>
> > >> Thank you,
> > >> Lada
>
> Wouldn't this work?
> http://us2.php.net/manual/ro/function.array-merge.php
> http://us2.php.net/manual/ro/function.array-merge-recursive.php
>
> Also, you don't need to do (and shouldn't do):
>
>         $arr1["$key"]
>
> Just make it
>
>         $arr1[$key]
>
> No " marks. The quotes are a waste of time and will slow down your script if
> you have many rows.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to