I need to merge a pair of MySQL queries from two databases each located on different servers into a single multidimensional associative array. Below is an example of the data I'm working.
The issue seems to be related to the resulting $data0 array values lacking a unique key (ideally the corresponding 'sku' value) to serve as an index for merging the data.
The desired output would be:
Array (
[0] => Array (
[SKU] => 101
[Description] => Test Item 1
[On Hand 1] => 3
[On Hand 2] => 7
)
[1] => Array (
[SKU] => 102
[Description] => Test Item 2
[On Hand 1] => 11
[On Hand 2] => 15
)
)
There may be a better way to handle the MySQL queries and desired array. If so, please let me know. Please provide any feedback as I'm a rank beginner and will take all the help I can get. Thanks!
<?
$query1 = "SELECT sku, description, on_hand as 'On Hand 1' FROM database1"
$queryresult1 = mysql_query($query1, $connection1);
$data1 = array();
while ($resultrow1 = mysql_fetch_assoc($queryresult1)) {
array_push($data1, $resultrow1);
}
$query2 = "SELECT sku, on_hand as 'On Hand 2' FROM database2"
$queryresult2 = mysql_query($query2, $connection2);
$data2 = array();
while ($resultrow2 = mysql_fetch_assoc($queryresult2)) {
array_push($data2, $resultrow2);
}
$data0 = array_merge($data1, $data2);
//example code for troubleshooting
$data1 = array('101' => array('SKU' => '101', 'Description' => 'Test Item 1', 'On Hand 1' => 3), '102' => array('SKU' => '102', 'Description' => 'Test Item 2', 'On Hand 1' => 11));
print_r($data1);
echo "<br /><br />";
$data2 = array('101' => array('SKU' => '101', 'On Hand 2' => 7), '102' => array('SKU' => '102', 'On Hand 2' => 15));
print_r($data2);
echo "<br /><br />";
$data0 = array_merge($data1, $data2);
print_r($data0);
?>