0

I have two multidimensional arrays. I want to check each item in array 1 and see if it's in array 2 and vice versa.

For example:

$arr1 = [
    [
        id => '1',
        order => '123238'
    ],
    [
        id => '2',
        order => '33278'
    ],
    [
        id => '3',
        order => '8892372'
    ]
];

$arr2 = [
    [
        id => '1',
        order => '349483'
    ],
    [
        id => '2',
        order => '9837283'
    ],
    [
        id => '3',
        order => '33278'
    ]
];

I want to check by order.

So something like this:

if($arr1['order'] does not exist in $arr2['order']) {
    echo 'abc';
}

if($arr2['order'] does not exist in $arr1['order']) {
    echo 'xyz';
}

I've thought about doing foreach loop for both but it won't let me know arr # it's missing from.

How can I achieve this?

5
  • Use in_array() function it should work for you. Commented Nov 27, 2019 at 12:25
  • One approach would be: map both arrays to their order and then use array_diffto compute the difference between both. Commented Nov 27, 2019 at 12:27
  • Will in_array work if the id is different? Commented Nov 27, 2019 at 12:30
  • 1
    Also, as a heads up, you should not use assumed strings as your array indices. Commented Nov 27, 2019 at 12:36
  • You should probably add some quotation marks to id and order where you define the array. Commented Nov 27, 2019 at 13:10

5 Answers 5

1

You can approach like this by using array_column and array_intersect_key

 $arr1 = array_column($arr1, null , 'order');
 $arr2 = array_column($arr2, null , 'order');
 print_r(array_intersect_key($arr1,$arr2));

Example :- https://3v4l.org/Imbao

Sign up to request clarification or add additional context in comments.

7 Comments

How do you get the ones that are in arr1 but not in arr2 and vice versa?
Yes this works but check this out: 3v4l.org/dWvMo. arr2 is not associative array anymore. How can I compare now?
@TimmyBalk check this out for more details : 3v4l.org/nTtpL
Okay thank you but what about this? 3v4l.org/dWvMo How can I make that work? The $arr2 is just regular array, not associative array. I just need that to work
@TimmyBalk in the second case you can use like this 3v4l.org/f2Ga5, 3v4l.org/hRvm8
|
1

Try something like this:

for ($i = 0; $i < 3; $i++) {
      for ($j = 0; $j < 3; $j++) {
            if (!in_array($arr1[$i]['order'], $arr2[$j])) {
                  echo "abc ";
            }
      }
}

It basically checks if each order of arr1 already exists in $arr2 and if it doesn't exist there, you get one abc as output.

Therefore you'd get this as output:

abc abc abc abc abc abc abc abc abc



Here's also a version with a foreach:

foreach ($arr1 as $arrone){
      foreach ($arr2 as $arrtwo) {
            if (!in_array($arrone['order'], $arrtwo)) {
                  echo "abc ";
            }
      }
}

3 Comments

It is also possible to do this with foreach too right?
Probably. Gimmie a sec I'll try doing it
@TimmyBalk I edited my answer, now there's one with foreach too
1

Ignoring your ids, build a list of order numbers from your second array, then loop through your initial array to check if the order number is not contained within:

Data:

$arr1 = [
    [
        id => '1',
        order => '123238'
    ],
    [
        id => '2',
        order => '33278'
    ],
    [
        id => '3',
        order => '8892372'
    ]
];

$arr2 = [
    [
        id => '1',
        order => '349483'
    ],
    [
        id => '2',
        order => '9837283'
    ],
    [
        id => '3',
        order => '33278'
    ]
];

Method:

foreach($arr2 as $item)
    $orders2[] = $item['order'];

foreach($arr1 as $item)
    if(!in_array($item['order'], $orders2))
        echo $item['order'], " order number is not contained\n";

Output:

123238 order number is not contained
8892372 order number is not contained

Comments

1

You can use either array_uintersect or array_udiff to find the values in each array that are either the same or different based on the value of the order key:

// values in both $arr1 and $arr2
print_r(array_uintersect($arr1, $arr2, function ($a, $b) { return $a['order'] - $b['order']; }));
// values in $arr1 but not $arr2
print_r(array_udiff($arr1, $arr2, function ($a, $b) { return $a['order'] - $b['order']; }));
// values in $arr2 but not $arr1
print_r(array_udiff($arr1, $arr2, function ($a, $b) { return $a['order'] - $b['order']; }));

Output:

Array
(
    [1] => Array
        (
            [id] => 2
            [order] => 33278
        )

)

Array
(
    [0] => Array
        (
            [id] => 1
            [order] => 123238
        )
    [2] => Array
        (
            [id] => 3
            [order] => 8892372
        )
)

Array
(
    [0] => Array
        (
            [id] => 1
            [order] => 123238
        )
    [2] => Array
        (
            [id] => 3
            [order] => 8892372
        )
)

Demo on 3v4l.org

7 Comments

Thank you but it would it be possible to show the id column too?
@TimmyBalk sure - but if the id value is different even though the order value is the same should that still be counted as the same value?
It should be counted as different values but still show both. Also what is this called in programming?
So just to be clear, for your sample data there would be no matching values between $arr1 and $arr2?
@TimmyBalk apologies for the radio silence... had to sleep. Based on the answer you accepted I think this might be a simpler method of achieving your desired result.
|
0

You might use array_uintersect with a custom callback function to check for the value or order to get all the intersections.

$intersect = array_uintersect($arr1, $arr2, function ($a, $b) {
    return strcmp($a['order'], $b['order']);
});

print_r($intersect);

$notInArr1 = array_uintersect($arr1, $arr2, function ($a, $b) {
    if ($a["order"] === $b["order"]) return -1;
    if ($a["order"] > $b["order"]) return 1;
    return 0;
});

print_r($notInArr1);

$notInArr2 = array_uintersect($arr2, $arr1, function ($a, $b) {
    if ($a["order"] === $b["order"]) return -1;
    if ($a["order"] > $b["order"]) return 1;
    return 0;
});

print_r($notInArr2);

php demo

Output

Array
(
    [1] => Array
        (
            [id] => 2
            [order] => 33278
        )

)
Array
(
    [0] => Array
        (
            [id] => 1
            [order] => 123238
        )

    [2] => Array
        (
            [id] => 3
            [order] => 8892372
        )

)
Array
(
    [0] => Array
        (
            [id] => 1
            [order] => 349483
        )

    [1] => Array
        (
            [id] => 2
            [order] => 9837283
        )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.