昨天我有一個功能是需要判斷生成的多個數組交集,也就是要判斷這些數組中是否存在交集了,下面我來給各位同學介紹php數組交集判斷程序代碼實例,有需要的朋友可參考.
需要判斷兩個數組是否有交集,第一個感覺PHP中應該有這個函數,果然:
array array_intersect(array array1,array array2[,arrayN…])
返回N個數組中的交集元素,如果是關聯數組可以用:array_intersect_assoc()
PHP案例如下:
數組的交集 array_intersect()
array_intersect()函數返回一個保留了鍵的數組,這個數組只由第一個數組中出現的且在其他每個輸入數組中都出現的值組成,其形式如下:
- <?php
- $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<$count;$i++){
- if(!array_intersect(explode(',', $projects[$i]['role']), explode(',', $projects[$i]['next_approve_role']))){
- unset($projects[$i]);
- continue;
- }
- }
- }
關聯數組的交集array_intersect_assoc(),代碼如下:
- <?php
- $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),以此為前提來模擬生成一些數據,代碼如下:
- <?php
- $rand = function() {
- $result = array();
- for ($i = 0; $i < 1000; $i++) {
- $result[] = mt_rand(1, 10000);
- }
- return $result;
- };
- $param_a = $rand();
- $param_b = $rand();
- ?>
注意:如果測試數據集過小的話,結論可能會出現不一致,先看看通過PHP內置方法array_intersect實現的性能,代碼如下:
- <?php
- $time = microtime(true);
- $result = array_intersect($param_a, $param_b);
- $time = microtime(true) - $time;
- echo "array_intersect: {$time}n";
- ?>
在優化之前,我們先來看看array_intersect一些特殊的地方,代碼如下:
- <?php
- $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實現的性能,代碼如下:
- <?php
- function int_array_intersect()
- {
- if (func_num_args() < 2) {
- 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 < $length_a && $j < $length_b; null) {
- if($a[$i] < $b[$j] && ++$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);
- }//開源代碼Vevb.com
- 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
新聞熱點
疑難解答