0

I try to implement a recursive array_diff, but I sill do not find a 100% working function.

I have two arrays:

Array
(
    [11] => Array
        (
            [id] => 24077
            [a] => 11
        )

    [22] => Array
        (
            [id] => 24078
            [a] => 22
        )

)

Array
(
    [11] => Array
        (
            [id] => 24077
            [a] => 11
        )

    [22] => Array
        (
            [id] => 24078
            [a] => 22
        )

    [27] => Array
        (
            [id] => 24080
            [a] => 27
        )

)

I tested both Function mentioned here: recursive array_diff()?

I call the functions via:

$diff = arrayRecursiveDiff($array1, $array2);
$diff = array_diff_recursive($array1, $array2);

I always get an empty array back. There's one element deleted in the first array, so I expect a result that shows me that one element is missing (maybe an array item with empty content?).

If I change the arrays in the function call:

$diff = arrayRecursiveDiff($array2, $array1);
$diff = array_diff_recursive($array2, $array1);

I'll get the new one [27]... but I need the way described above.

Do you have any ideas? How would you try to find a way to solve this problem?

Community
  • 1
  • 1
Marco
  • 3,470
  • 4
  • 23
  • 35
  • So, you want to find out if a element has been deleted? – Epodax Sep 15 '15 at 09:37
  • correct. The array's above are only a apart of a bigger array – Marco Sep 15 '15 at 09:39
  • Why not just count both of the arrays and if one is larger than the other you know a value as been deleted? – Epodax Sep 15 '15 at 09:39
  • It's quite difficult to explain... I've a huge array with many sub-arrays an some single values and i have to check the difference between them. It works fine if you add new items and all differences on silimar keys in both arrays are found, but i don't get a hint, when an element is deleted. – Marco Sep 15 '15 at 09:48
  • But the two arrays are (if no value is deleted), completely identical? – Epodax Sep 15 '15 at 09:54
  • yes, they use the same keys. – Marco Sep 15 '15 at 09:55
  • Would [array_diff_key](http://php.net/manual/en/function.array-diff-key.php) not work? – Epodax Sep 15 '15 at 10:00
  • not in the way i expected. – Marco Sep 15 '15 at 10:18

0 Answers0