PHP is a commonly used server-side programming language that allows developers to easily process and operate various data types. One of the most commonly used data types is arrays. In PHP, searching an array is a very basic and common operation. Next, let's explore how to search arrays in PHP.
1. Introduction to array search in PHP
In PHP, an array refers to a set of ordered elements, which can be stored in the form of key-value pairs, where each element There is an associated key and corresponding value. When you need to find these array elements, you need to use array search.
Array search refers to searching for a specified value or key in an existing array to determine whether the target value or key exists or does not exist in the array. PHP provides many array search methods, including linear search, binary search, recursive search, etc.
2. Linear search of arrays in PHP
The simplest search method in PHP is linear search, which is a method of finding the target key or value by traversing the entire array. The basic idea of linear search is to compare the target value and key one by one starting from the first element of the array. If the target value or key is found, the corresponding index or value is returned; if the target is not found after traversing the entire array, return " false" means the search failed.
The following is a sample code implementation:
$arr = array("apple", "orange", "banana", "grape"); $target = "banana"; for($i=0;$i<count($arr);$i++){ if($arr[$i] == $target){ echo "目标值 " . $target . " 在数组中的索引是 " . $i; break; } }
In the above code, we define an array $arr, and then use a loop to iterate through each element in the array. When the target value is found, use the "echo" statement to output the index of the target value in the array, and then use the "break" statement to end the loop.
3. Binary search of arrays in PHP
In PHP, if the array is ordered, you can use binary search for faster search. Binary search is a comparison-based search algorithm and a typical application of divide-and-conquer thinking.
The basic idea of binary search is: divide the ordered array into two parts, find a value in the middle of the array, and compare the target with this value, if the target is smaller than the value, then in the first half (left side) Recursive search; if the target is greater than this value, search recursively in the second half (right side); if the target is equal to this value, return the corresponding index or value.
The following is a sample code implementation:
function binary_search($arr,$target){ $low = 0; $high = count($arr) - 1; while($low <= $high) { $middle = intval(($low + $high)/2); if($arr[$middle] == $target) { return $middle; } elseif ($arr[$middle] > $target) { $high = $middle -1; } else { $low = $middle + 1; } } return -1; } $arr = array(1,3,5,7,9,11); $target = 7; echo "目标值 " . $target . " 在数组中的索引是 " . binary_search($arr,$target);
In the above code, we define a function binary_search, which receives an ordered array and a target value as parameters and returns the target The index of the value in the array. Loops and conditional statements are used in the function to implement the binary search algorithm.
4. Recursive search of arrays in PHP
Recursive search is a simple but relatively inefficient search method. It traverses the entire array by recursively calling its own function and finds the target value. or key. The basic idea of recursive search is: if the array is empty, return "false"; otherwise, check whether the first array element is the target value or key, and if so, return the corresponding index or value; otherwise, call its own function recursively to process the remaining array elements except the first element.
The following is a sample code implementation:
function search($arr,$target) { if(empty($arr)){ return false; } if($arr[0] == $target){ return 0; } else { $index = search(array_slice($arr,1),$target); if($index === false) { return false; } else { return $index+1; } } } $arr = array("apple", "orange", "banana", "grape"); $target = "orange"; echo "目标值 " . $target . " 在数组中的索引是 " . search($arr,$target);
In the above code, we define a function search, which receives an array and a target value as parameters, and returns the target value in Index in the array. The function uses a recursive call to its own function to traverse the entire array and find the target value or key.
5. Summary
The above is an introduction to the method of searching arrays in PHP. We can choose the appropriate search algorithm according to different needs: for unordered arrays, we can choose linear search or recursive search; for ordered arrays, we can choose binary search to improve search efficiency. In actual development, it is very important to choose a method according to the actual situation, and the impact on performance is also very large.
The above is the detailed content of How to search an array in php. For more information, please follow other related articles on the PHP Chinese website!