php數組交集判斷與最佳化程式碼_PHP教程

來源:互聯網
上載者:User
昨天我有一個功能是需要判斷產生的多個數組交集,也就是要判斷這些數組中是否存在交集了,下面我來給各位同學介紹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數組交集判斷程...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.