PHP合并两个没有重复的数组
I have a table which contains the following:
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.
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.
As already mentioned, array_unique()
could be used, but only when dealing with simple data. The objects are not so simple to handle.
When php tries to merge the arrays, it tries to compare the values of the array members. If a member is an object, it cannot get its value and uses the spl hash instead.
Simply told if you have two objects, instances of the very same class and if one of them is not a reference to the other one - you will end up having two objects, no matter the value of their properties.
To be sure that you don't have any duplicates within the merged array, Imho you should handle the case on your own.
Also if you are going to merge multidimensional arrays, consider using array_merge_recursive()
over array_merge()
.
array_unique(array_merge($array1,$array2), SORT_REGULAR);
It will also work without SORT_REGULAR