In PHP, the data query can classify a one-dimensional array lookup, and multidimensional array lookup, if it is a simple one-dimensional array we can directly use In_array, Array_search and the history of the example, if it is a multidimensional array need to use other methods.
For one-dimensional arrays we can do the following
The In_array ' function searches the array for the given value. In_array (value,array,type) type is optional. If set to true, checks whether the searched data is the same as the type of the array's value.
The Array_key_exists ' array_key_exists () function determines whether the specified key exists in an array, returns TRUE if the key exists, or false otherwise. Array_key_exists (Key,array)
The Array_search ' Array_search () function, like In_array (), looks for a key value in the array. If the value is found, the key name of the matching element is returned. Returns False if it is not found. Array_search (value,array,strict)
From this point of view, when the amount of data is not large, such as less than 1000, look for what kind of line, will not become a bottleneck;
When the amount of data is relatively large, it is more appropriate to use array_key_exists.
Of course, the array_key_exists occupy a large amount of memory here, after measuring
Binary method finds whether an array contains an element, is compatible with the pros and cons, and the code implements:
The code is as follows |
Copy Code |
$searchValue = (int) $_get[' key ']; function search (array $array, $value) { $max = count ($array)-1; $min = 0; $isAscSort = $array [$min] < $array [$max]; while (TRUE) { $sum = $min + $max; $midKey = (int) ($sum%2 = = 1? ceil ($sum/2): $sum/2); if ($max < $min) { return-1; } else if ($value = = $array [$midKey]) { return 1; } else if ($value > $array [$midKey]) { $isAscSort? $min = $midKey + 1: $max = $midKey-1; } else if ($value < $array [$midKey]) { $isAscSort? $max = $midKey-1: $min = $midKey +1; } } } $array = Array ( ' 4 ', ' 5 ', ' 7 ', ' 8 ', ' 9 ', ' 10 ', ' 11 ', ' 12 ' ); Positive order Echo Search ($array, $searchValue); Reverse Rsort ($array); Echo Search ($array, $searchValue); |
Example Two
PHP find array I small element
The code is as follows |
Copy Code |
#随机选择第i小的数字, implemented with random fast rows #交换元素 Function Swap (& $arr, $i, $j) { $temp = $arr [$i]; $arr [$i] = $arr [$j]; $arr [$j] = $temp; } #随机划分 Function Randomized_partition (& $arr, $begin, $end) { $rand _inx = rand ($begin, $end); Swap ($arr, $begin, $rand _inx); return partition ($arr, $begin, $end); } #划分 function partition (& $arr, $begin, $end) { #以第一个元素作为中枢元素 $pivot = $begin; $low = $begin; $high = $end; while ($low < $high) { while ($low < $high && $arr [$low] <= $arr [$pivot]) { $low + +; } while ($low < $high && $arr [$high] >= $arr [$pivot]) { $high--; } Swap ($arr, $low, $high); } #交换中枢元素 if ($arr [$pivot] < $arr [$low]) { $low--; } Swap ($arr, $pivot, $low); return $low; } #快速排序, there's no use here. Function Quick_sort (& $arr, $begin, $end) { $q = Randomized_partition ($arr, $begin, $end); if ($q > $begin) { Quick_sort ($arr, $begin, $q-1); } if ($q < $end) { Quick_sort ($arr, $q + 1, $end); } } #选取第i小的数 Function Randomized_select (& $arr, $begin, $end, $i) { if ($begin = = $end) { return $arr [$begin]; } $q = Randomized_partition ($arr, $begin, $end); $k = $q-$begin + 1; #k代表小于等于q的元素个数 if ($k = = $i) {#如果k =i, stating that Q is the small element coordinate of the I return $arr [$q]; } else if ($i < $k) {#如果i <> Return Randomized_select ($arr, $begin, $q-1, $i); } else {#第i小的元素位于q的右边, find the I-k small element on the right Return Randomized_select ($arr, $q + 1, $end, $i-$k); } } $arr = Array (1, 5, 3, 7, 0, 0, 8, 4, 2, 9, 11); $t = Randomized_select ($arr, 0, Count ($arr)-1, 8); Print_r ("The 8th minimum element: {$t}"); echo " "; Quick_sort ($arr, 0, Count ($arr)-1); Print_r ($arr); ?> |
http://www.bkjia.com/PHPjc/631562.html www.bkjia.com true http://www.bkjia.com/PHPjc/631562.html techarticle in PHP data query can classify a one-dimensional array lookup, and multidimensional array lookup, if it is a simple one-dimensional array we can directly use In_array, Array_search with the history of the example, such as ...