dev
dev

Reputation: 745

Compare two arrays, based on child array's element value in PHP

Here is my sample array. I got another one like this one with EXACT same number of elements (25 array elements in the case). The app gives me 2 IDs from those arrays for example: 355 from array #1 and 888 from array#2 and I have to compare if the corresponding parent array id is equal.

If my the array where ID: 355 is the 0th element from the parent array just like 888 from array#2. So to make sure if the 0 = 0.

Array#1

Array
(
[0] => Array
    (
        [id] => 355
        [name] => 1
        [desc] => 1
        [price] => 0
    )

[1] => Array
    (
        [id] => 356
        [name] => 1
        [desc] => 2
        [price] => 0
    )

[2] => Array
    (
        [id] => 357
        [name] => 2
        [desc] => 3D
        [price] => 0
    )
...

Here is Array #2

Array
(
[0] => Array
    (
        [id] => 888
        [name] => 15
        [desc] => 1D
        [price] => 0
    )

[1] => Array
    (
        [id] => 889
        [name] => 16
        [desc] => 2D
        [price] => 0
    )

[2] => Array
    (
        [id] => 890
        [name] => 17
        [desc] => 3D
        [price] => 0
    )
...

I really couldn't figure out how to explain it better.

Any ideas are welcome.

Upvotes: 0

Views: 588

Answers (1)

Peter
Peter

Reputation: 9123

$match = true;
foreach ($array1 as $key => $value) {
    foreach ($value as $subkey => $subvalue) {
        if ($array1[$key][$subkey] != $array2[$key][$subkey]) {
            $match = false;
        }
    }
}

if (false === $match) {
    // The arrays are not the same
}

Upvotes: 2

Related Questions