Home  >  Article  >  php教程  >  PHP array_intersect函数

PHP array_intersect函数

PHP中文网
PHP中文网Original
2016-05-25 17:15:061083browse

PHP array_intersect函数

/**
 * 
 * 自定义的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 class="brush:php;toolbar:false">";
print_r(my_array_intersect($array1, $array2));
echo "<pre/>";

 以上就是PHP array_intersect函数的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn