-3

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:

$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 :

$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

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. Thanks to 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);
paraplouis
  • 11
  • 4

2 Answers2

0

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
$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:

Array
(
    [0] => Array
        (
            [ref] => bee898d8739aa8b2212
            [place] => Berlin
        )
    [1] => Array
        (
            [ref] => abc123abc12ab1234ab
            [place] => London
        )
)
arkascha
  • 41,620
  • 7
  • 58
  • 90
0

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 = [...$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
        )

)
XMehdi01
  • 5,538
  • 2
  • 10
  • 34