多维关联数组 - 差异

时间:2011-11-30 23:01:11

标签: php

如何比较多维关联数组。

有些功能如:http://www.php.net/manual/en/function.array-diff.php#98680

例如,ArrayDiff应该是比较Array1和Array2的结果。显示具有不同价值和新元素的元素。

Array1
(
    [0] => Array
        (
            [item] => 39.00
            [time] => 0.00
        )
    [1] => Array
        (
            [item] => 49.00
        )
    [2] => Array
        (
            [time] => 0.00
            [Value] => 0
        )
    [3] => Array
        (
            [item] => 49.00
            [time] => 0.00
        )
)

Array2
(
    [0] => Array
        (
            [item] => 39.00
            [time] => 10.00
        )
    [1] => Array
        (
            [item] => 49.00
        )
    [2] => Array
        (
            [time] => 0.00
            [Value] => 0
        )
    [3] => Array
        (
            [item] => 49.00
            [time] => 0.00
            [Value] => 3
        )
)


ArrayDiff
(
    [0] => Array
        (
            [time] => 10.00
        )
    [3] => Array
        (
            [Value] => 3
        )
)

1 个答案:

答案 0 :(得分:1)

这应该可以帮到你的大部分地方:

$arrayDiff = array_map('array_diff_assoc', $array1, $array2);

如果你想摆脱空的(相等的)元素,也许可以跟array_filter($arrayDiff)一起使用。