<?php The collection of two arrays $arr 1 = Array (' A ', ' B ', ' C ', ' d ', ' e ', ' f '); $arr 2 = Array (' A ', ' a ', ' e ', ' A ', ' P ', ' a ', ' a ', ' e '); $arr 2 = Array (' A ', ' a ', ' a ', ' a '); $ilength = count ($arr 1); $jlength = count ($arr 2);
/** * Intersection of two arrays * @param array $arr 1 * @param array $arr 2 * @autho Zhaoya * @return Array $arr */ function Jiaoji ($arr 1, $arr 2) { $ilength = count ($arr 1); $jlength = count ($arr 2); for ($i =0; $i < $jlength; $i + +) { for ($j =0; $j < $ilength; $j + +) { if ($arr 2[$i] = = $arr 1[$j]) { $arr [] = $arr 2[$i]; Break } } } return $arr; } $arr 3 = array (1,10,10,5,90,50,90); $arr 4 = array (10,23,50,100,110,80); Echo ' <pre> '; $time 1 = microtime ();
/* $arr 3 = __deleterepeat ($arr 3); $arr 4 = __deleterepeat ($arr 4); $arr 5 = Bingji ($arr 3, $arr 4); $arrsort = Sort_array ($arr 5);
After the experiment proved that the first and the set, and then in the removal of duplicate values, and then sorted, this speed will be a little faster and first remove the duplicate values of the two arrays, and in the set, in the sort of words, the speed is slower. $arr 5 = Bingji ($arr 3, $arr 4); $arr 5 = __deleterepeat ($arr 5); $arrsort = Sort_array ($arr 5);
$time 2 = Microtime (); echo $time 1, ' echo $time 2, ' echo $time 2-$time 1;
Print_r ($arrsort); Remove duplicate values (first method) __deleterepeat ($arr 2);
/** * Remove duplicate values (first method) * @param array $array * @return Array $tmparr * @author Zhaoya */ function __deleterepeat ($array) { $count = count ($array); for ($i = 0; $i < $count; $i + +) { $change = false; for ($j = $i +1; $j < $count; $j + +) { if ($array [$i] = = $array [$j]) { $change =true; Break } } if ($change ==false) { $tmparr [] = $array [$i]; } } return $tmparr; }
Remove duplicate values the second method
$arrayshift = _delrepeat ($arr 2); $tmparray =array (); /*** * Remove duplicate values of one-dimensional array * @param array $arr * @return Array $tmparray; * @author Zhaoya */ function _delrepeat ($arr) { for ($i =0; $i <count ($arr); $i + +) { if (inarray ($arr [$i], $tmp)) { $tmparray [] = $arr [$i]; } } return $tmparray; }
/** * Find whether the variable is inside this array * @param integer $num * @param array $arr * @author Zhaoya * @return Boolean * */ function inarray ($num, $arr) { if ($arr) { for ($i =0; $i <count ($arr); $i + +) { if ($arr [$i] = = $num) { return false; } return true; } } return true; }
/** * The collection of two arrays * @param array $arr 1 arrays 1 * @param array $arr 2 Arrays 2 * @author Zhaoya * @return Array $arr 1 */ function Bingji ($arr 1, $arr 2) { $ilength = count ($arr 1); $jlength = count ($arr 2); for ($i =0; $i < $jlength; $i + +) { $change =false; for ($j =0; $j < $ilength; $j + +) { if ($arr 2[$i] = = $arr 1[$j]) { $change = true; Break } } if ($change = = False) { $arr 1[] = $arr 2[$i]; } } return $arr 1; }
/** * Array sorting from small to large * Array $arr @param array * @author Zhaoya * @return Array $arr */ function Sort_array ($arr) { $length = count ($arr);
for ($i =0; $i < $length; $i + +) { for ($j = $i +1; $j < $length; $j + +) { if ($arr [$i] > $arr [$j]) { $tmp = $arr [$i]; $arr [$i] = $arr [$j]; $arr [$j] = $tmp; } } } return $arr; }
?> |