我如何在php中比较两个数组

时间:2016-05-30 11:27:14

标签: php arrays array-difference

我想要比较两个多维数组。这就是它们的样子。我想要有所作为。我试过阵列差异,但它似乎没有用。继承人是我的代码

Array1
(
  [0] => Array
  (
    [name] => john
    [surname] => elvis
    [idnumber] => 01148015
  )
  [1] => Array
  (
    [name] => sammy
    [surname] => dwayne
    [idnumber] => 01148046
  )
)

Array2
(
  [0] => Array
  (
    [name] => john
    [surname] => elvis
    [idnumber] => 01148015
  )
)

$difference = array_diff($Array1, $Array2);
print_r($difference);

2 个答案:

答案 0 :(得分:1)

使用array_intersect()代替:

$result = array_intersect($array1, $array2);

答案 1 :(得分:0)

试试这个:

您还可以在此处看到:http://php.net/manual/en/function.array-diff-assoc.php#111675

array_diff_assoc_recursive($a1, $a2);

function array_diff_assoc_recursive($array1, $array2)
{
    foreach($array1 as $key => $value)
    {
        if(is_array($value))
        {
            if(!isset($array2[$key]))
            {
                $difference[$key] = $value;
            }
            elseif(!is_array($array2[$key]))
            {
                $difference[$key] = $value;
            }
            else
            {
                $new_diff = array_diff_assoc_recursive($value, $array2[$key]);
                if($new_diff != FALSE)
                {
                    $difference[$key] = $new_diff;
                }
            }
        }
        elseif(!isset($array2[$key]) || $array2[$key] != $value)
        {
            $difference[$key] = $value;
        }
    }
    return !isset($difference) ? 0 : $difference;
}