dongmei9203 2016-09-16 03: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 03: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部