获取两个二维数组之间的差异

Get the diff between two 2-dimensional arrays

我需要根据 StudentId 列值来区分 $array1$array2

 $array1 = array(
     array('StudentId' => 1),
     array('StudentId' => 2)
 );
 $array2 = array(
     array('StudentId' => 1)
);

输出应该是:

Array ( [0] => Array ( [StudentId] => 2 ) )

这是您要找的吗?

$array1 = array( array( 'StudentId' => 1 ), array( 'StudentId' => 2 ) );
$array2 = array( array( 'StudentId' => 1 ));

var_dump(array_diff_key($array1, $array2));

输出:

array(1) { [1]=> array(1) { ["StudentId"]=> int(2) } }

看看http://php.net/manual/en/function.array-diff.php

array array_diff ( array $array1 , array $array2 [, array $... ] )

Compares array1 against one or more other arrays and returns the values in array1 that are not present in any of the other arrays.

我按如下方式解决了这个问题,

$array1 = array( array( 'StudentId' => 1 ), array( 'StudentId' => 2 ) );
$array2 = array( array( 'StudentId' => 1 ));
foreach($array1 as $a=>$val){
     if(in_array($val,$array2)){
          unset($array1[$a]);
     }
}

var_dump(array_values($array1));