array_intersect_assoc通过比较数组的键和值,计算数组的交集。?
- array_intersect 比较值
- array_intersect_assoc比较键和值
/**
* Computes the intersection of arrays with additional index check
* @link https://php.net/manual/en/function.array-intersect-assoc.php
* @param array $array1 <p>
* The array with master values to check.
* </p>
* @param array $array2 <p>
* An array to compare values against.
* </p>
* @param array $_ [optional]
* @return array an associative array containing all the values in
* array1 that are present in all of the arguments.
* @meta
*/
function array_intersect_assoc(array $array1, array $array2, array $_ = null) { }
示例:
$array1 = [
'c2' => 'ccc',
'a1' => 'aaa',
'b1' => 'bbb'
];
$array2 = [
'c2' => 'ccc',
'a2' => 'aaa',
'b2' => 'bbb2'
];
$result = array_intersect_assoc($array1, $array2);
var_dump($result);
//结果
//array(1) {
// 'c2' =>
// string(3) "ccc"
//}
|