dpmfur2635 2015-08-24 18:22
浏览 126
已采纳

在PHP中合并来自不同数据库的两个mysql查询

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);
?>
  • 写回答

4条回答 默认 最新

  • doujuxin7392 2015-08-24 19:46
    关注

    By looping trough each key in $data1, you can retreive the value from $data2 by using the current $key. This will give you the result you want:

    $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));
    $data2 = array('101' => array('SKU' => '101', 'On Hand 2' => 7), '102' => array('SKU' => '102', 'On Hand 2' => 15));
    
    $data0 = array();
    foreach($data1 as $key => $data){
        $data['On Hand 2'] = $data2[$key]['On Hand 2'];
        $data0[] = $data;
    }
    unset($data1, $data2);
    
    print_r($data0);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?