昨天我有一個功能是需要判斷產生的多個數組交集,也就是要判斷這些數組中是否存在交集了,下面我來給各位同學介紹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<$count;$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 < 1000; $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() < 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); } 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
http://www.bkjia.com/PHPjc/445278.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445278.htmlTechArticle昨天我有一個功能是需要判斷產生的多個數組交集,也就是要判斷這些數組中是否存在交集了,下面我來給各位同學介紹php數組交集判斷程...