Home > php教程 > php手册 > body text

php数组交集判断与优化程序代码

WBOY
Release: 2016-06-13 11:19:51
Original
1059 people have browsed it

昨天我有一个功能是需要判断生成的多个数组交集,也就是要判断这些数组中是否存在交集了,下面我来给各位同学介绍php数组交集判断程序代码实例,有需要的朋友可参考。  

需要判断两个数组是否有交集,第一个感觉PHP中应该有这个函数,果然:

array array_intersect(array array1,array array2[,arrayN…])

返回N个数组中的交集元素,如果是关联数组可以用array_intersect_assoc()

PHP案例如下:

数组的交集 array_intersect()
array_intersect()函数返回一个保留了键的数组,这个数组只由第一个数组中出现的且在其他每个输入数组中都出现的值组成。其形式如下:

 代码如下 复制代码


$fruit1 = array("Apple","Banana","Orange");
$fruit2 = array("Pear","Apple","Grape");
$fruit3 = array("Watermelon","Orange","Apple");
$intersection = array_intersect($fruit1, $fruit2, $fruit3);
print_r($intersection);
// 输出 Array ( [0] => Apple )
?>

我的应用如下:

 代码如下 复制代码

if($user->role != 1){
            $count = count($projects);
            for($i=0;$i                 if(!array_intersect(explode(',', $projects[$i]['role']),  explode(',', $projects[$i]['next_approve_role']))){
                    unset($projects[$i]);
                    continue;
                }
            }
 }

关联数组的交集 array_intersect_assoc()

 代码如下 复制代码

$fruit1 = array("red"=>"Apple","yellow"=>"Banana","orange"=>"Orange");
$fruit2 = array("yellow"=>"Pear","red"=>"Apple","purple"=>"Grape");
$fruit3 = array("green"=>"Watermelon","orange"=>"Orange","red"=>"Apple");
$intersection = array_intersect_assoc($fruit1, $fruit2, $fruit3);
print_r($intersection);

// output
// Array ( [red] => Apple )
?>

数组交集的优化

假定每个参数会包含一千个左右的产品ID(int),以此为前提来模拟生成一些数据:

 代码如下 复制代码

$rand = function() {
    $result = array();

    for ($i = 0; $i         $result[] = mt_rand(1, 10000);
    }

    return $result;
};

$param_a = $rand();
$param_b = $rand();

?>

注意:如果测试数据集过小的话,结论可能会出现不一致。

先看看通过PHP内置方法array_intersect实现的性能:

 代码如下 复制代码

$time = microtime(true);

$result = array_intersect($param_a, $param_b);

$time = microtime(true) - $time;

echo "array_intersect: {$time}n";

?>

在优化之前,我们先来看看array_intersect一些特殊的地方:

 代码如下 复制代码

$param_a = array(1, 2, 2);
$param_b = array(1, 2, 3);

var_dump(
    array_intersect($param_a, $param_b),
    array_intersect($param_b, $param_a)
);

?>

array_intersect($param_a, $param_b): 1, 2, 2
array_intersect($param_b, $param_a): 1, 2
也就是说,如果在第一个数组参数中有重复元素的话,则array_intersect会返回所有满足条件的重复元素。改写array_intersect的时候最好兼容这些功能。

下面看看通过自定义方法int_array_intersect实现的性能:

 代码如下 复制代码

function int_array_intersect()
{
    if (func_num_args()         trigger_error('param error', E_USER_ERROR);
    }

    $args = func_get_args();

    foreach ($args AS $arg) {
        if (!is_array($arg)) {
            trigger_error('param error', E_USER_ERROR);
        }
    }

    $intersect = function($a, $b) {
        $result = array();

        $length_a = count($a);
        $length_b = count($b);

        for ($i = 0, $j = 0; $i             if($a[$i]                 continue;
            }

            if($a[$i] > $b[$j] && ++$j) {
                continue;
            }

            $result[] = $a[$i];

            if (isset($a[$next = $i + 1]) && $a[$next] != $a[$i]) {
                ++$j;
            }
            ++$i;
        }

        return $result;
    };

    $result = array_shift($args);
    sort($result);

    foreach ($args as $arg) {
        sort($arg);
        $result = $intersect($result, $arg);
    }

    return $result;
}

$time = microtime(true);

$result = int_array_intersect($param_a, $param_b);

$time = microtime(true) - $time;

echo "int_array_intersect: {$time}n";

?>

直觉上,我们肯定会认为内置函数快于自定义函数,但本例中结果恰恰相反:

array_intersect: 0.023918151855469
int_array_intersect: 0.0026049613952637


source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!