比较两个数组,基于PHP中的子数组的元素值

时间:2015-12-03 11:08:38

标签: php arrays compare

这是我的示例数组。我得到了另一个像这样一个具有完全相同数量的元素(在这种情况下是25个数组元素)。 该应用程序为我提供了来自这些数组的2个ID,例如:来自数组#1的355和来自数组#2的888,我必须比较相应的父数组id是否相等。

如果我的数组ID:355是父数组中的第0个元素,就像数组#2中的888一样。所以要确定0 = 0。

阵列#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
    )
...

这是阵列#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
    )
...

我真的无法弄清楚如何更好地解释它。

欢迎任何想法。

1 个答案:

答案 0 :(得分:2)

$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
}