-2

I know that there is a lot of similar questions here, but I really think this one is different from the others. I have 2 arrays like this:

Array 1

Array
(
    [0] => Array
        (
            [name] => aaaa
            [projectId] => 0
            [area] => AreaX
        )

    [1] => Array
        (
            [name] => cccc
            [projectId] => 2
            [area] => 
        )

)

Array 2

Array
(
    [0] => Array
        (
            [name] => cccc
            [projectId] => 1
            [area] => 
        )

    [1] => Array
        (
            [name] => aaaa
            [projectId] => 0
            [area] => AreaX
        )

)

What I need

Array
(
    [1] => Array
        (
            [projectId] => 2
        )

)

I read and tested a lot of suggestion from similar questions, but all of them return the whole array or something completely different. Some of related functions that I tried: 1, 2, 3, 4, 5, 6, 7. I realize that the main problem is related to the position of the arrays. When I rearrange them the functions work perfectly (I'm currently using the number 3), but the situation I have here doesn't allow this reorganization.

leonardofmed
  • 842
  • 3
  • 13
  • 47
  • 2
    whats the logic? –  Aug 15 '19 at 23:11
  • Is there anything that uniquely identifies the inner arrays? Otherwise, how can you tell that one of them is supposed to be an altered version of another when any or all of the keys could have been changed? – Don't Panic Aug 15 '19 at 23:13

1 Answers1

1

You can achieve that by using array_diff, array_column and array_map:

$col = "projectId";
$res = array_diff(array_column($a, $col), array_column($b, $col));
$res = array_map(function($id) use ($col) {return [$col => $id];}, $res);

Reference: array-diff, array-column, array_map

Live example: 3v4l

dWinder
  • 11,597
  • 3
  • 24
  • 39