Php provides a summary of various methods for searching array elements. In php, data queries can be classified into one-dimensional array queries and multi-dimensional array queries. if it is a simple one-dimensional array, we can directly use in_array, array_search, and traversal for instances, for example, in php, data queries can be classified into one-dimensional array queries and multi-dimensional array queries. if a simple one-dimensional array is used, we can directly use in_array, array_search, and traversal for instances, if it is a multi-dimensional array, you need to use other methods.
The following operations can be performed on one-dimensional arrays:
The in_array 'function searches for the given value in the array. In_array (value, array, type) type is optional. If this parameter is set to true, check whether the data to be searched is of the same type as the value of the array.
The array_key_exists 'Array _ key_exists () function checks whether a specified key exists in an array. if the key exists, true is returned. otherwise, false is returned. Array_key_exists (key, array)
The array_search' array _ search () function is the same as the in_array () function. you can find a key value in the array. If this value is found, the key name of the matching element is returned. If not found, false is returned. Array_search (value, array, strict)
From this point of view, when the data volume is small, such as less than 1000, finding which row is used will not become a bottleneck;
When the data volume is large, it is more appropriate to use array_key_exists.
Of course, array_key_exists occupies a large amount of memory.
The binary method is used to check whether an array contains a certain element. it is compatible with positive and negative order. the code is implemented as follows:
| The code is as follows: |
|
$ 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' ); // Forward Echo search ($ array, $ searchValue ); // Reverse order Rsort ($ array ); Echo search ($ array, $ searchValue ); |
Example 2
PHP searches for small I elements in the array.
| The code is as follows: |
|
# Randomly select a number smaller than I, and implement it in a random fast rank. # Swap element Function swap (& $ arr, $ I, $ j ){ $ Temp = $ arr [$ I]; $ Arr [$ I] = $ arr [$ j]; $ Arr [$ j] = $ temp; } # Random Division Function randomized_partition (& $ arr, $ begin, $ end ){ $ Rand_tables = rand ($ begin, $ end ); Swap ($ arr, $ begin, $ rand_inx ); Return partition ($ arr, $ begin, $ end ); } # Division Function partition (& $ arr, $ begin, $ end ){ # Use the first element as the central element $ Begin = $ begin; $ Low = $ begin; $ High = $ end; While ($ low <$ high ){ While ($ low <$ high & $ arr [$ low] <= $ arr [$ slow]) { $ Low ++; } While ($ low <$ high & $ arr [$ high] >=$ arr [$ hour]) { $ High --; } Swap ($ arr, $ low, $ high ); } # Central element exchange If ($ arr [$ scheme] <$ arr [$ low]) { $ Low --; } Swap ($ arr, $ scheme, $ low ); Return $ low; } # Fast sorting, not used 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 ); } } # Select the number smaller than 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 indicates the number of elements less than or equal to q. If ($ k = $ I) {# if k = I, q is the element coordinate smaller than I. Return $ arr [$ q]; } Else if ($ I <$ k) {# if I Return randomized_select ($ arr, $ begin, $ q-1, $ I ); } Else {# The element smaller than I is located on the right of q. In this case, find the element smaller than I-k 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 ); ?> |
Example, array_search and traversal come to the instance, such...