This is a discussion on combining 2 arrays within the PHP General forums, part of the PHP Programming Forums category; Hi list! I read data from 2 databases and for the purpose of displaying the information at my web interface ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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')) 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 |
|
|||
|
array_merge() function?
If your db is MySQL, look at this thread, Google being your friend: http://lists.mysql.com/mysql/204616 Cheers - Milesx On 10/16/07, Ladislav Andel <ladaan@iptel.org> 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')) > > 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 > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > |
|
|||
|
Miles Thompson wrote:
> array_merge() function? > > If your db is MySQL, look at this thread, Google being your friend: > http://lists.mysql.com/mysql/204616 > > Thanks for quick reply. Array_merge doesn't do what I want. And also those databases are at different locations.. Not the same machines. I found something here http://www.linux.com/articles/52390 but not sure if it is what I want but will read up. Thanks, Lada > Cheers - Milesx > > On 10/16/07, Ladislav Andel <ladaan@iptel.org> 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')) >> >> 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 >> >> -- >> PHP General Mailing List (http://www.php.net/) >> To unsubscribe, visit: http://www.php.net/unsub.php >> >> >> > > |
|
|||
|
Ladislav Andel wrote:
> Miles Thompson wrote: >> array_merge() function? >> >> If your db is MySQL, look at this thread, Google being your friend: >> http://lists.mysql.com/mysql/204616 >> >> > Thanks for quick reply. > Array_merge doesn't do what I want. > > And also those databases are at different locations.. Not the same > machines. > I found something here http://www.linux.com/articles/52390 > but not sure if it is what I want but will read up. > This is not what I'm looking for either.. (replicating). Data at each database are mostly different. > Thanks, > Lada > > >> Cheers - Milesx >> >> On 10/16/07, Ladislav Andel <ladaan@iptel.org> 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')) >>> >>> 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 >>> >>> -- >>> PHP General Mailing List (http://www.php.net/) >>> To unsubscribe, visit: http://www.php.net/unsub.php >>> >>> >>> >> >> > |
|
|||
|
On Tue, 2007-10-16 at 16:42 +0200, Ladislav Andel wrote: > 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')) > You could try something like: $result[] = $arrayDB1; $result[] .= $arrayDB2; If I understand the question correctly. --Paul All Email originating from UWC is covered by disclaimer http://www.uwc.ac.za/portal/uwc2006/...imer/index.htm |
|
|||
|
On Tue, 16 Oct 2007, Paul Scott wrote:
> You could try something like: > > $result[] = $arrayDB1; > $result[] .= $arrayDB2; That .= doesn't do what you think it does when used with arrays. -- Greg Donald Cyberfusion Consulting http://cyberfusionconsulting.com/ |
|
|||
|
On 10/16/07, Greg Donald <greg@cyberfusionconsulting.com> wrote:
> > On Tue, 16 Oct 2007, Paul Scott wrote: > > You could try something like: > > > > $result[] = $arrayDB1; > > $result[] .= $arrayDB2; > > That .= doesn't do what you think it does when used with arrays. Take this same concept (of creating a new array) and see if you can use array_merge. I know you mentioned earlier that it wasn't what you were looking for... however, maybe the combination will work??? Good luck, ~Philip |
|
|||
|
On 10/16/07, Ladislav Andel <ladaan@iptel.org> 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 > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > |
|
|||
|
nope, merge is not doing what I want and also using FEDERATED
<http://localhost/phpmyadmin/server_engines.php?engine=FEDERATED&token=d18a0e0d e85e891b3558f84f6585994f> engine in mysql I think is not the simplest way to go or even not appropriate. Anyhow, I did it an ugly way ( tablearray follows DB queries with the same fields; one row of a query is e.g. count=5, server_name="Asterisk" ) Variable dbs is an array with MySQL URIs. Lada function get_data($dbs, $tablearray, $query) { $id = 0; $result = array(); $records = 0; $db_select = 0; for ($c = 0; $c < $len; $c++) { $db =& DB::Connect( $dbs[$db_select] ); if (PEAR::isError($db)) { echo "Can't connect to DB".$db_select."<br>"; } else { $res = $db->query($query, array() ); while( $res->fetchInto( $row ) ) { $cols = count($row); if ($db_select > 0) { for ($i=0; $i < $records; $i++) { if (in_array($row[1],$result[$i])) { $result[$i][$tablearray[0]] = $row[0] + $result[$i][$tablearray[0]]; break; } else { if (!@$entered) { @$entered = True; for ($b=0; $b<$cols; $b++) { $result[$id][$tablearray[$b]] = $row[$b]; } $id++; } } } @$entered = False; } else { for ($i=0; $i<$cols; $i++) { $result[$id][$tablearray[$i]] = $row[$i]; } $id++; } } } $db_select++; $records = count($result); return $result; } Philip Thompson wrote: > On 10/16/07, Greg Donald <greg@cyberfusionconsulting.com> wrote: > >> On Tue, 16 Oct 2007, Paul Scott wrote: >> >>> You could try something like: >>> >>> $result[] = $arrayDB1; >>> $result[] .= $arrayDB2; >>> >> That .= doesn't do what you think it does when used with arrays. >> > > > Take this same concept (of creating a new array) and see if you can use > array_merge. I know you mentioned earlier that it wasn't what you were > looking for... however, maybe the combination will work??? > > Good luck, > ~Philip > > |
|
|||
|
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 <ladaan@iptel.org> 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 >> >> -- >> PHP General Mailing List (http://www.php.net/) >> To unsubscribe, visit: http://www.php.net/unsub.php >> >> >> > > |
![]() |
| Thread Tools | |
| Display Modes | |
|
|