Jump to content

Merging 2 mysql resultant arrays


chrisfane

Recommended Posts

im currently executing sql queries on two seperate mysql servers.  i take back each of these results into individual tables to display them. 

 

What i wanted to do was perform a merge of the two arrays, so that i could order the results by my "TimeStamp" field.

 

When i try to perform a basic " $mergedresult = array_merge($result, $isaresult); " i get the following.

 

"

Warning: array_merge() [function.array-merge]: Argument #1 is not an array in C:\Program Files\xampplite\htdocs\seatpub.php on line 164

 

Warning: array_merge() [function.array-merge]: Argument #2 is not an array in C:\Program Files\xampplite\htdocs\seatpub.php on line 164  "

 

Im rather new to this and so havent delt with arrays before, but im under the impression my results from the sql queries, are Assoc arrays.  can these be merged ? do i need to use an alternate php function ?

 

 

Thanks for any help

 

Chris

Link to comment
https://forums.phpfreaks.com/topic/62003-merging-2-mysql-resultant-arrays/
Share on other sites

you need to create an array from each result set

 

I suspect you are doing someting like...

 

<?php
$result = mysql_query('SELECT...');
$isaresult = mysql_query('SELECT...');

$mergedresult = array_merge($result, $isaresult);
?>

 

If so you have missed out the array formation...

 

<?php
$result = mysql_query('SELECT...');
$isaresult = mysql_query('SELECT...');
$resultarr = array();
while($row = mysql_fetch_assoc($result))
{
foreach($row as $key => $val)
{
  $resultarr[$key][] = $val;
}
}

$isaresult = array();
while($row = mysql_fetch_assoc($result))
{
foreach($row as $key => $val)
{
  $isaresult[$key][] = $val;
}
}

$mergedresult = array_merge($resultarr, $isaresultarr);
?>

 

the field names present in the two datasest should be the same - otherwise you may give yourself a bit of a headache!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.