[英]How to get the difference between two multidimensional arrays of associative arrays in PHP?
我的問題是我有兩個 arrays 的大小不一定相同,它們包含始終相同大小的關聯 arrays,例如:
$a = array([0] => array(["ref"]=> string(19) "ygz121byr46dl4795eu", ["place"]=> string(5) "Paris", ["date"]=> string(25) "2012-03-14T16:42:47+01:00", ...),
[1] => array(["ref"]=> string(19) "cer123bur45dl4795ef", ["place"]=> string(8) "New-York", ["date"]=> string(25) "2015-06-14T05:05:49+01:00", ...),
[2] => array(["ref"]=> string(19) "abc123abc12ab1234ab", ["place"]=> string(6) "London", ["date"]=> string(25) "2020-02-12T08:03:39+01:00", ...),
...);
$b = array([0] => array(["ref"]=> string(19) "ygz121byr46dl4795eu", ["place"]=> string(5) "Paris", ["date"]=> string(25) "2012-03-14T16:42:47+01:00", ...),
[1] => array(["ref"]=> string(19) "cer123bur45dl4795ef", ["place"]=> string(8) "New-York", ["date"]=> string(25) "2015-06-14T05:05:49+01:00", ...),
...);
我嘗試了幾種方法,但似乎沒有任何效果,或者我只得到沒有值的鍵。 我想在一個數組中得到差異($a 中的所有元素都不存在 $b),所以這里的結果是:
$result = array( array(["ref"]=> string(19) "abc123abc12ab1234ab", ["place"]=> string(6) "London", ["date"]=> string(25) "2020-02-12T08:03:39+01:00", ...),
...);
“ref”鍵是每個數組的唯一字段,所以我需要在 $result 中所有“ref”鍵不在 $b 中的文檔
編輯:我發現( https://stackoverflow.com/a/42530586/15742179 )通過使用array_diff(),array_map(),json_encode()和decode()方法來做到這一點。 感謝 Ifnot
// compare all value
$result = array_diff(array_map('json_encode', $a), array_map('json_encode', $b));
// decode the result
$result = array_map('json_decode', $result);
您需要遍歷要過濾的元素集或過濾器集。 這將是一種可能的方法:
<?php
$a = [
["ref" => "bee898d8739aa8b2212", "place" => "Berlin"],
["ref" => "ygz121byr46dl4795eu", "place" => "Paris"],
["ref" => "cer123bur45dl4795ef", "place" => "New-York"],
["ref" => "abc123abc12ab1234ab", "place" => "London"]
];
$b = [
["ref" => "ygz121byr46dl4795eu", "place" => "Paris"],
["ref" => "cer123bur45dl4795ef", "place" => "New-York"],
];
$filter = array_column($b, "ref");
$result = array_combine(array_column($a, "ref"), $a);
array_walk($filter, function($entry) use (&$result) {
unset($result[$entry]);
});
print_r(array_values($result));
另一種方法是使用 php 的 array_filter() function:
<?php
$a = [
["ref" => "bee898d8739aa8b2212", "place" => "Berlin"],
["ref" => "ygz121byr46dl4795eu", "place" => "Paris"],
["ref" => "cer123bur45dl4795ef", "place" => "New-York"],
["ref" => "abc123abc12ab1234ab", "place" => "London"]
];
$b = [
["ref" => "ygz121byr46dl4795eu", "place" => "Paris"],
["ref" => "cer123bur45dl4795ef", "place" => "New-York"],
];
array_walk($b, function($filter) use (&$a) {
$a = array_filter($a, function($entry) use ($filter) {
return $entry["ref"] != $filter["ref"];
});
});
print_r($a);
output 顯然是:
Array
(
[0] => Array
(
[ref] => bee898d8739aa8b2212
[place] => Berlin
)
[1] => Array
(
[ref] => abc123abc12ab1234ab
[place] => London
)
)
通過將它們合並在一起來制作一個數組$c
,使用函數array_column
和array_unique
獲取唯一引用,並通過array_diff_assoc
獲取重復項,然后使用array_diff
輕松獲取差異引用,然后遍歷數組並僅在$diff
數組中獲取它們的引用。
$c = [...$a, ...$b];
$arr = array_column($c, "ref");
$unique = array_unique($arr);
$duplicates = array_diff_assoc($arr, $unique);
$diff = array_diff($unique, $duplicates);
$output = [];
foreach ($c as $d) {
foreach ($d as $key => $value) {
if(in_array($d[$key], $diff))
$output[] = $d;
}
}
echo "<pre>",print_r($output),"</pre>";
Output :
Array
(
[0] => Array
(
[ref] => bee898d8739aa8b2212
[place] => Berlin
)
[1] => Array
(
[ref] => abc123abc12ab1234ab
[place] => London
)
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.