国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php數組交集判斷與優化程序代碼

2024-09-04 11:46:26
字體:
來源:轉載
供稿:網友

昨天我有一個功能是需要判斷生成的多個數組交集,也就是要判斷這些數組中是否存在交集了,下面我來給各位同學介紹php數組交集判斷程序代碼實例,有需要的朋友可參考.

需要判斷兩個數組是否有交集,第一個感覺PHP中應該有這個函數,果然:

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

返回N個數組中的交集元素,如果是關聯數組可以用:array_intersect_assoc()

PHP案例如下:

數組的交集 array_intersect()

array_intersect()函數返回一個保留了鍵的數組,這個數組只由第一個數組中出現的且在其他每個輸入數組中都出現的值組成,其形式如下:

  1. <?php 
  2. $fruit1 = array("Apple","Banana","Orange"); 
  3. $fruit2 = array("Pear","Apple","Grape"); 
  4. $fruit3 = array("Watermelon","Orange","Apple"); 
  5. $intersection = array_intersect($fruit1$fruit2$fruit3); 
  6. print_r($intersection); 
  7. // 輸出 Array ( [0] => Apple ) 
  8. ?> 

我的應用如下:

  1. if($user->role != 1){ 
  2.             $count = count($projects); 
  3.             for($i=0;$i<$count;$i++){ 
  4.                 if(!array_intersect(explode(','$projects[$i]['role']),  explode(','$projects[$i]['next_approve_role']))){ 
  5.                     unset($projects[$i]); 
  6.                     continue
  7.                 } 
  8.             } 
  9.  } 

關聯數組的交集array_intersect_assoc(),代碼如下:

  1. <?php 
  2. $fruit1 = array("red"=>"Apple","yellow"=>"Banana","orange"=>"Orange"); 
  3. $fruit2 = array("yellow"=>"Pear","red"=>"Apple","purple"=>"Grape"); 
  4. $fruit3 = array("green"=>"Watermelon","orange"=>"Orange","red"=>"Apple"); 
  5. $intersection = array_intersect_assoc($fruit1$fruit2$fruit3); 
  6. print_r($intersection); 
  7. // output 
  8. // Array ( [red] => Apple ) 
  9. ?> 

數組交集的優化,假定每個參數會包含一千個左右的產品ID(int),以此為前提來模擬生成一些數據,代碼如下:

  1. <?php 
  2. $rand = function() { 
  3.     $result = array(); 
  4.     for ($i = 0; $i < 1000; $i++) { 
  5.         $result[] = mt_rand(1, 10000); 
  6.     } 
  7.     return $result
  8. }; 
  9. $param_a = $rand(); 
  10. $param_b = $rand(); 
  11. ?> 

注意:如果測試數據集過小的話,結論可能會出現不一致,先看看通過PHP內置方法array_intersect實現的性能,代碼如下:

  1. <?php 
  2. $time = microtime(true); 
  3. $result = array_intersect($param_a$param_b); 
  4. $time = microtime(true) - $time
  5. echo "array_intersect: {$time}n"
  6. ?> 

在優化之前,我們先來看看array_intersect一些特殊的地方,代碼如下:

  1. <?php 
  2. $param_a = array(1, 2, 2); 
  3. $param_b = array(1, 2, 3); 
  4. var_dump( 
  5.     array_intersect($param_a$param_b), 
  6.     array_intersect($param_b$param_a
  7. ); 
  8. ?> 
  9. array_intersect($param_a$param_b): 1, 2, 2  
  10. array_intersect($param_b$param_a): 1, 2  

也就是說,如果在第一個數組參數中有重復元素的話,則array_intersect會返回所有滿足條件的重復元素,改寫array_intersect的時候最好兼容這些功能.

下面看看通過自定義方法int_array_intersect實現的性能,代碼如下:

  1. <?php 
  2. function int_array_intersect() 
  3.     if (func_num_args() < 2) { 
  4.         trigger_error('param error', E_USER_ERROR); 
  5.     } 
  6.     $args = func_get_args(); 
  7.     foreach ($args AS $arg) { 
  8.         if (!is_array($arg)) { 
  9.             trigger_error('param error', E_USER_ERROR); 
  10.         } 
  11.     } 
  12.     $intersect = function($a$b) { 
  13.         $result = array(); 
  14.         $length_a = count($a); 
  15.         $length_b = count($b); 
  16.         for ($i = 0, $j = 0; $i < $length_a && $j < $length_b; null) { 
  17.             if($a[$i] < $b[$j] && ++$i) { 
  18.                 continue
  19.             } 
  20.             if($a[$i] > $b[$j] && ++$j) { 
  21.                 continue
  22.             } 
  23.             $result[] = $a[$i]; 
  24.             if (isset($a[$next = $i + 1]) && $a[$next] != $a[$i]) { 
  25.                 ++$j
  26.             } 
  27.             ++$i
  28.         } 
  29.         return $result
  30.     }; 
  31.     $result = array_shift($args); 
  32.     sort($result); 
  33.     foreach ($args as $arg) { 
  34.         sort($arg); 
  35.         $result = $intersect($result$arg); 
  36.     }//開源代碼Vevb.com 
  37.     return $result
  38. $time = microtime(true); 
  39. $result = int_array_intersect($param_a$param_b); 
  40. $time = microtime(true) - $time
  41. echo "int_array_intersect: {$time}n"
  42. ?> 

直覺上,我們肯定會認為內置函數快于自定義函數,但本例中結果恰恰相反:

array_intersect: 0.023918151855469

int_array_intersect: 0.0026049613952637

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 盐边县| 西昌市| 岱山县| 平武县| 新余市| 雷山县| 维西| 泰来县| 孟津县| 青阳县| 安福县| 博兴县| 营口市| 老河口市| 广饶县| 青浦区| 晋中市| 巩义市| 伊川县| 合肥市| 东兴市| 昭觉县| 济源市| 白朗县| 息烽县| 湖南省| 额尔古纳市| 乐亭县| 康乐县| 芒康县| 衡山县| 佛教| 资源县| 鄂温| 滨州市| 商水县| 新化县| 赣榆县| 白山市| 黄平县| 南澳县|