This article summarizes the intersection of arrays in php, the Union, and the deduplication of arrays, as well as the sorting functions from small to large, are almost all friends who need to operate Arrays for reference. This article summarizes the intersection of arrays in php, the Union, and the deduplication of arrays, as well as the sorting functions from small to large, are almost all friends who need to operate Arrays for reference.
Script ec (2); script
The Code is as follows: |
|
// Union of two Arrays $ Arr1 = array ('A', 'B', 'C', 'D', 'E', 'F '); // $ Arr2 = array ('A', 'A', 'E', 'A', 'A', 'P '); $ Arr2 = array ('A', 'A '); $ Ilength = count ($ arr1 ); $ Jlength = count ($ arr2 ); /** * Intersection of two Arrays * @ Param array $ arr1 * @ Param array $ arr2 * @ Autho zhaoya * @ Return array $ arr */ Function jiaoji ($ arr1, $ arr2) { $ Ilength = count ($ arr1 ); $ Jlength = count ($ arr2 ); For ($ I = 0; $ I <$ jlength; $ I ++) { For ($ j = 0; $ j <$ ilength; $ j ++) { If ($ arr2 [$ I] = $ arr1 [$ j]) { $ Arr [] = $ arr2 [$ I]; Break; } } } Return $ arr; } $ Arr3 = array (, 90 ); $ Arr4 = array (50,100,110, 80 ); Echo''; $ Time1 = microtime (); /* $ Arr3 = _ deleterepeat ($ arr3 ); $ Arr4 = _ deleterepeat ($ arr4 ); $ Arr5 = bingji ($ arr3, $ arr4 ); $ Arrsort = sort_array ($ arr5 );*/ /// After experiment, it is proved that the Union is set first, then duplicate values are removed, and then sorted. This speed will be faster. /// Delete the duplicate values of the two arrays first. In the Union set and sorting, this speed is slow. $ Arr5 = bingji ($ arr3, $ arr4 ); $ Arr5 = _ deleterepeat ($ arr5 ); $ Arrsort = sort_array ($ arr5 ); $ Time2 = microtime (); Echo $ time1 ,''; Echo $ time2 ,''; Echo $ time2-$ time1; Print_r ($ arrsort ); // Remove duplicate values (method 1) // _ Deleterepeat ($ arr2 ); /** * Remove duplicate values (method 1) * @ 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; } // Method 2 for removing duplicate values $ Arrayshift = _ delrepeat ($ arr2 ); $ Tmparray = array (); /*** * Remove duplicate values of one-dimensional arrays * @ Param array $ arr * @ Return array $ tmparray; * @ Author zhaoya */ Function _ delrepeat ($ arr) { For ($ I = 0; $ I
{ If (inarray ($ arr [$ I], $ tmp )) { $ Tmparray [] = $ arr [$ I]; } } Return $ tmparray; } /** * Check whether the variable is in this array. * @ Param integer $ num * @ Param array $ arr * @ Author zhaoya * @ Return boolean * */ Function inarray ($ num, $ arr) { If ($ arr) { For ($ I = 0; $ I
{ If ($ arr [$ I] = $ num) { Return false; } Return true; } } Return true; } /** * Union of two Arrays * @ Param array $ arr1 array 1 * @ Param array $ arr2 array 2 * @ Author zhaoya * @ Return array $ arr1 */ Function bingji ($ arr1, $ arr2) { $ Ilength = count ($ arr1 ); $ Jlength = count ($ arr2 ); For ($ I = 0; $ I <$ jlength; $ I ++) { $ Change = false; For ($ j = 0; $ j <$ ilength; $ j ++) { If ($ arr2 [$ I] = $ arr1 [$ j]) { $ Change = true; Break; } } If ($ change = false) { $ Arr1 [] = $ arr2 [$ I]; } } Return $ arr1; } /** * Array sorting from small to large * @ Param array $ arr 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; } ?> |