dongmei9203 2016-09-16 11:21
浏览 42
已采纳

PHP合并两个没有重复的数组

I have a table which contains the following:

enter image description here

I need to extract the data into one array which can contain only the date where there are no duplicate keys or values.

The first query returns:

Array ( 
[0] => Array ( [UniqueID] => NXLHR01011474021550 [Room] => 0101 [AuditBy] => navexdemo2 [AuditDate] => 2016-09-16 11:26:00 [SeqID] => SeqID1306 [Status] => 1 [WaterHot] => 67.0 ) 
[1] => Array ( [UniqueID] => NXLHR01011474021550 [Room] => 0101 [AuditBy] => navexdemo2 [AuditDate] => 2016-09-16 11:26:00 [SeqID] => SeqID1307 [Status] => 0 [WaterHot] => ) 
[2] => Array ( [UniqueID] => NXLHR01021474021587 [Room] => 0102 [AuditBy] => navexdemo2 [AuditDate] => 2016-09-16 11:26:00 [SeqID] => SeqID1306 [Status] => 0 [WaterHot] => 65.0 ) 
[3] => Array ( [UniqueID] => NXLHR01021474021587 [Room] => 0102 [AuditBy] => navexdemo2 [AuditDate] => 2016-09-16 11:26:00 [SeqID] => SeqID1307 [Status] => 1 [WaterHot] => ) ) 

I then use:

do {
    $Fields1[] = $row_Water1; 
} while ($row_Water1 = mysql_fetch_assoc($Water1));

The second query returns:

Array ( 
[0] => Array ( [UniqueID] => NXLHR01011474021550 [Room] => 0101 [AuditBy] => navexdemo2 [AuditDate] => 2016-09-16 11:26:00 [SeqID] => SeqID1306 [Status] => 1 [WaterCold] => ) 
[1] => Array ( [UniqueID] => NXLHR01011474021550 [Room] => 0101 [AuditBy] => navexdemo2 [AuditDate] => 2016-09-16 11:26:00 [SeqID] => SeqID1307 [Status] => 0 [WaterCold] => 18.0 ) 
[2] => Array ( [UniqueID] => NXLHR01021474021587 [Room] => 0102 [AuditBy] => navexdemo2 [AuditDate] => 2016-09-16 11:26:00 [SeqID] => SeqID1306 [Status] => 0 [WaterCold] => ) 
[3] => Array ( [UniqueID] => NXLHR01021474021587 [Room] => 0102 [AuditBy] => navexdemo2 [AuditDate] => 2016-09-16 11:26:00 [SeqID] => SeqID1307 [Status] => 1 [WaterCold] => 21.0 ) ) 

I then use:

do {
    $Fields2[] = $row_Water2; 
} while ($row_Water2 = mysql_fetch_assoc($Water2));

Both $Fields1 and $Fields2 contain my data ready to merge.

I then use $Water = array_unique (array_merge ($Fields1, $Fields2)); to try and create an array which contains no duplicate keys or values. When I run the script the new merged array contains:

Array ( 
[0] => Array ( [UniqueID] => NXLHR01011474021550 [Room] => 0101 [AuditBy] => navexdemo2 [AuditDate] => 2016-09-16 11:26:00 [SeqID] => SeqID1306 [Status] => 1 [WaterHot] => 67.0 ) ) 

My question is how can I produce the merged array to contain the following:

    Array ( 
[0] => Array ( [UniqueID] => NXLHR01011474021550 [Room] => 0101 [AuditBy] => navexdemo2 [AuditDate] => 2016-09-16 11:26:00 [SeqID] => SeqID1306 [Status] => 1 [WaterHot] => 67.0 [SeqID] => SeqID1307 [WaterCold] => 18.0) ) 
[1] => Array ( [UniqueID] => NXLHR01021474021587 [Room] => 0102 [AuditBy] => navexdemo2 [AuditDate] => 2016-09-16 11:26:00 [SeqID] => SeqID1306 [Status] => 1 [WaterHot] => 65.0 [SeqID] => SeqID1307 [WaterCold] => 21.0) ) 

I have tried to do this so many ways without any success, can anyone see a way this could be done.

  • 写回答

2条回答 默认 最新

  • douguanya4248 2016-09-16 11:38
    关注

    With the examples you've given, I can think up something like this:

    $array = array();
    
    foreach ($Fields1 as $row) {
        if (!isset($array[$row['UniqueID']])) {
            $array[$row['UniqueID']] = $row;
        } else {
            if (!is_null($row['WaterHot'])) {
                $array[$row['UniqueID']]['WaterHot'] = $row['WaterHot'];
            }
    
            if (!is_null($row['WaterCold'])) {
                $array[$row['UniqueID']]['WaterCold'] = $row['WaterCold'];
            }
        }
    }
    
    foreach ($Fields2 as $row) {
        if (!is_null($row['WaterHot'])) {
            $array[$row['UniqueID']]['WaterHot'] = $row['WaterHot'];
        }
    
        if (!is_null($row['WaterCold'])) {
            $array[$row['UniqueID']]['WaterCold'] = $row['WaterCold'];
        }
    }
    

    It's pretty straight forward, and merge the WaterHot and WaterCold fields, where the UniqueID column is the unique key.
    The UniqueID column will also be the key of the merged array. If you don't want this, and want a numeric column, you'll need to use array_values on the resulting array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法