简体   繁体   English

如何获得PHP中关联arrays的两个多维arrays之间的区别?

[英]How to get the difference between two multidimensional arrays of associative arrays in PHP?

My problem is that I have two arrays that are not necessarily the same size that contain associative arrays which are always of the same size, for example:我的问题是我有两个 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", ...), 
     ...);

I tried several methods but nothing seems to work, or I only get the keys without the values.我尝试了几种方法,但似乎没有任何效果,或者我只得到没有值的键。 I want to get the difference (all elements in $a not present $b) in an array, so here the result would be:我想在一个数组中得到差异($a 中的所有元素都不存在 $b),所以这里的结果是:

$result = array( array(["ref"]=> string(19) "abc123abc12ab1234ab", ["place"]=> string(6) "London", ["date"]=> string(25) "2020-02-12T08:03:39+01:00", ...), 
          ...);

The "ref" key is a unique field for each array, so I needed in $result all the documents whose "ref" key is not in $b “ref”键是每个数组的唯一字段,所以我需要在 $result 中所有“ref”键不在 $b 中的文档

EDIT: I found ( https://stackoverflow.com/a/42530586/15742179 ) a way to do it by using array_diff(), array_map(), json_encode() and decode() method.编辑:我发现( https://stackoverflow.com/a/42530586/15742179 )通过使用array_diff(),array_map(),json_encode()和decode()方法来做到这一点。 Thanks to Ifnot感谢 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);

You need to iterate over either the set of elements to be filtered or over the set of filters.您需要遍历要过滤的元素集或过滤器集。 This would be a possible approach:这将是一种可能的方法:

<?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));

Another approach would be to use php's array_filter() function:另一种方法是使用 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);

The output obviously is: output 显然是:

Array
(
    [0] => Array
        (
            [ref] => bee898d8739aa8b2212
            [place] => Berlin
        )
    [1] => Array
        (
            [ref] => abc123abc12ab1234ab
            [place] => London
        )
)

Make one array $c by merging them together, get uniques ref using functions array_column and array_unique and also get duplicates by array_diff_assoc , then easily get differences ref using array_diff , then loop over the array and take only their ref in $diff array.通过将它们合并在一起来制作一个数组$c ,使用函数array_columnarray_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 : Output

Array
(
    [0] => Array
        (
            [ref] => bee898d8739aa8b2212
            [place] => Berlin
        )

    [1] => Array
        (
            [ref] => abc123abc12ab1234ab
            [place] => London
        )

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM