复制代码 代码如下:
<?php
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
?>
这使得 $result 成为:
Array
(
[a] => green
[0] => red
)
复制代码 代码如下:
/**
*
* 自定义的array_intersect
* 如果求的是一维数组的交集这个函数比系统的array_intersect快5倍
*
* @param array $arr1
* @param array $arr2
* @author LIUBOTAO 2010-12-13上午11:40:20
*
*/
function my_array_intersect($arr1,$arr2)
{
for($i=0;$i<sizeof($arr1);$i++)
{
$temp[]=$arr1[$i];
}
for($i=0;$i<sizeof($arr1);$i++)
{
$temp[]=$arr2[$i];
}
sort($temp);
$get=array();
for($i=0;$i<sizeof($temp);$i++)
{
if($temp[$i]==$temp[$i+1])
$get[]=$temp[$i];
}
return $get;
}
$array1 = array("green", "red", "blue");
$array2 = array("green", "yellow", "red");
echo "<pre>";
print_r(my_array_intersect($array1, $array2));
echo "<pre/>";
复制代码 代码如下:
<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
print_r($result);
?>
复制代码 代码如下:
Array
(
[1] => blue
)
新闻热点
疑难解答