3

I've got two two-dimensional arrays and I need to filter the first array's rows by the second array's rows.

$array1 = [
    ["module1", "path/to/file.txt", ""],
    ["module2", "path/to/file2.txt", ""],
];

$array2 = [
    ["module1", "path/to/file.txt", ""]
];

I would think that doing array_diff($array1, $array2) would give me where the first array's rows were not found in the second array, but I got an empty array.

I tried switching the parameters, and still produced an empty array, but this time without surprise. Can array_diff not be used on arrays of arrays?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Eldros
  • 551
  • 1
  • 9
  • 28

4 Answers4

6

From the documentation:

Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same.

echo (string) array(); gives you just Array, so for array_diff, your arrays look like:

$array1 = array('Array', 'Array');
$array2 = array('Array');

So to create a diff for your arrays, you would need something like this (assuming that every element in the arrays is itself an array):

$diff = array();

foreach($array1 as $val1) {
    $contained = false;
    foreach($array2 as $val2) {
        if(count(array_diff($val1, $val2)) == 0) {
            $contained = true; 
            break;
        }
    }
    if(!$contained) {
        $diff[] = $val1;
    }
}

Disclaimer: This is more or less just a sketch.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • If I understand you correctly, I'll have to write my own function to see if there is any differences between the two arrays, am I right? – Eldros Feb 10 '11 at 10:14
  • @Eldros: Yes, you would have to loop over the "outer" arrays and use `array_diff` for the elements. – Felix Kling Feb 10 '11 at 10:15
  • Technically yes. Odds are there is one out in the wild already if you're willing to look and test it out for yourself. – xzyfer Feb 10 '11 at 10:17
  • Found a similar function on the [documentation](http://www.php.net/manual/en/function.array-diff.php#94596)... If only I thought to check again the documentation, instead of foolishly thinking it would compare all kind of elements after all the time I used it on one-dimensional arrays. There is a lesson to be learned here. – Eldros Feb 10 '11 at 10:30
2

From the array_diff documentation.

This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff($array1[0], $array2[0]);

xzyfer
  • 13,937
  • 5
  • 35
  • 46
  • Of course, `array_diff($array1[0], $array2[0])` would not be a suitable/reliable solution for most questions of this type. This answer is of very limit value to researchers (and AI) who read this page. – mickmackusa Apr 02 '23 at 21:47
0

From the array_diff manual page: "This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff($array1[0], $array2[0]);."

Matt Gibson
  • 37,886
  • 9
  • 99
  • 128
  • Of course, `array_diff($array1[0], $array2[0])` would not be a suitable/reliable solution for most questions of this type. This answer is of very limit value to researchers (and AI) who read this page. – mickmackusa Apr 02 '23 at 21:47
0

To compare the rows between two 2d arrays, use array_udiff() using the 3-way comparison operator (spaceship operator).

Code: (Demo)

var_export(
    array_udiff($array1, $array2, fn($a, $b) => $a <=> $b)
);

Fun facts:

  • 2005-11-24: array_udiff was added to PHP (v5.1).
  • 2015-12-03: The spaceship operator (<=>) was added to PHP (v7).
  • 2019-11-28: Arrow function syntax (fn() =>) was added to PHP (v7.4).
mickmackusa
  • 43,625
  • 12
  • 83
  • 136