我从foreach循环得到了这个结果。 我尝试使用来自StackOverflow中的答案的foreach循环遍历数组,但是我在另一个foreach循环中执行它时遇到了麻烦。
Array
(
[0] => Array
(
[referenceUid] => FF875951-87CB-942F-84A2-46C620BF07C8
[itemUid] => 4CD23391-AD8C-C2FB-EF4A-46093D8A37AE
[total] => 300.00
)
)
Array
(
[0] => Array
(
[referenceUid] => D267A795-E142-C25B-E042-D8A519B57DF4
[itemUid] => 4CD23391-AD8C-C2FB-EF4A-46093D8A37AE
[total] => 150.00
)
)
Array
(
[0] => Array
(
[referenceUid] => D267A795-E142-C25B-E042-D8A519B57DF4
[itemUid] => 4CD23391-AD8C-C2FB-EF4A-46093D8A37AE
[total] => 150.00
)
)
我想要的是将数组与referenceUid列上的重复值合并。像这样:
Array
(
[0] => Array
(
[referenceUid] => FF875951-87CB-942F-84A2-46C620BF07C8
[itemUid] => 4CD23391-AD8C-C2FB-EF4A-46093D8A37AE
[total] => 300.00
)
)
Array
(
[0] => Array
(
[referenceUid] => D267A795-E142-C25B-E042-D8A519B57DF4
[itemUid] => 4CD23391-AD8C-C2FB-EF4A-46093D8A37AE
[total] => 150.00
)
[1] => Array
(
[referenceUid] => D267A795-E142-C25B-E042-D8A519B57DF4
[itemUid] => 4CD23391-AD8C-C2FB-EF4A-46093D8A37AE
[total] => 150.00
)
)
答案 0 :(得分:1)
您可以构造一个新的(合并的)数组并循环输入以组装新结构。
一个重要的考虑因素是使用公共密钥(referenceUid
)作为新阵列中的数组键,以便您可以轻松地引用它。如果你不想在最后,只需重置数组键,例如$out = array_values($out)
。
以下是一个例子:
$output = array();
foreach ($input as $values) {
$key = $values['referenceUid'];
$output[$key][] = $values;
}
// Don't want the referenceUid in the keys? Reset them:
$output = array_values($output);
答案 1 :(得分:-2)
//both arrays will be merged including duplicates
$result = array_merge( $array1, $array2 );
//duplicate objects will be removed
$result = array_map("unserialize", array_unique(array_map("serialize", $result)));
//array is sorted on the bases of id
sort( $result );`