Think before you speak, read before you think.

php array-intersect取两个数组交集

by

in

http://php.net/manual/en/function.array-intersect.php#84286

后面的数组与第一个数组比较,返回交集是一个数组,且下标是第一个数组的下标

$array1 = array(2, 4, 6, 8, 10, 12);
$array2 = array(1, 2, 3, 4, 5, 6);

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

结果

array(3) { [0]=> int(2) [1]=> int(4) [2]=> int(6) }
array(3) { [1]=> int(2) [3]=> int(4) [5]=> int(6) }


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *