Rumah > Artikel > pembangunan bahagian belakang > PHP 数组比较合并,求效率
php 数组
$arr1 = array(1, 3, 5, 9,34,128,129);
$arr1 = array(1, 3, 5, 9,34,128,129);$arr2 = array(1, 3, 6, 22,33, 128);$arr3 = array(128,78,56,33,5,1);$t = array_count_values(array_merge($arr1, $arr2, $arr3));arsort($t);print_r(array_keys($t));Array
$t = array_count_values(array_merge($arr1, $arr2, $arr3));$res = array();for($max=max($t); $max>0; $max--) $res = array_merge($res, array_keys($t, $max));print_r($res);Array
嗯,明白,还是版主给力啊~我昨晚也生搬硬套一个,
array_diff不给力啊,所以如此冗余
$arr1 = array(1,2,3,4,5,6,7,8,9,10);$arr2 = array(1,8,9,15,100);$arr3 = array(1,8,15,200);$res = array();$resHigh = array();//高度相似$resMiddle = array();//中$resLow = array();//低$res = array_merge($arr1,$arr2,$arr3);$res = my_array_unique($res); //排除重复$resHigh = array_intersect($arr1,$arr2,$arr3); //交集$resLow = array_merge(array_diff($arr1,$arr2,$arr3),array_diff($arr2,$arr1,$arr3),array_diff($arr3,$arr1,$arr2));//组合差集$res = array_diff($res,$resHigh); //排除High$resMiddle = array_diff($res,$resLow); //排除Low$res = array_merge($resHigh,$resMiddle,$resLow);function my_array_unique($array){//排除重复 $out = array(); foreach ($array as $key=>$value) { if (!in_array($value, $out)){ $out[$key] = $value; } } return $out;} echo "<pre class="brush:php;toolbar:false">";print_r($res);exit;/*( [0] => 1 [1] => 8 [2] => 9 [3] => 15 [4] => 2 [5] => 3 [6] => 4 [7] => 5 [8] => 6 [9] => 7 [10] => 10 [11] => 100 [12] => 200)*/