In PHP, determining the position of an element in an array is a very common operation. This article will introduce several methods to determine the position of elements in an array.
Method 1: Use the array_search() function
In PHP, you can directly use the array_search() function to find the position of an element in the array, and the return value is the key value corresponding to the position of the element. If not found, return false.
Sample code:
$arr = array('apple', 'banana', 'orange'); $pos = array_search('banana', $arr); echo $pos; // 输出 1
In the above example, use the array_search() function to find the position of the element 'banana' in the array $arr, and the return value is 1.
It should be noted that using this method can only determine the position of the first element in the array that meets the conditions. If there are multiple elements that meet the conditions, only the position of the first element can be obtained.
Method 2: Use array_keys() function
array_keys() function can return an array of the positions of all elements in an array whose key values are equal to the specified value. The key name in the returned array corresponds to the element. Location. If not found, an empty array is returned.
Sample code:
$arr = array('apple', 'banana', 'orange', 'banana'); $pos = array_keys($arr, 'banana'); print_r($pos); // 输出 Array ( [0] => 1 [1] => 3 )
In the above example, use the array_keys() function to find the position of the element 'banana' in the array $arr, and the return value is an array containing positions 1 and 3 .
It should be noted that this method can only determine the positions of all elements that meet the conditions, and cannot obtain specific element values or other information.
Method 3: Use foreach() loop
Use foreach() loop to traverse the array, you can judge the elements in the array one by one, and record the position information of the elements at the same time.
Sample code:
$arr = array('apple', 'banana', 'orange', 'banana'); foreach($arr as $key => $value) { if($value == 'banana') { echo $key; // 输出 1 3 } }
In the above example, use foreach() to loop through the array $arr, and determine whether the elements are equal to 'banana' one by one. If they are equal, the position of the element is output.
It should be noted that this method can obtain the value, position and other information of specific elements, but the efficiency is low, especially for large arrays, the time complexity is high.
Method 4: Use the array_reduce() function
The array_reduce() function can perform reduction operations on all elements of the array, while recording the position information of each element. It can reduce the elements in the array that meet the conditions The elements and position information are stored as an array, and then the position information of an element is obtained by traversing the array.
Sample code:
$arr = array('apple', 'banana', 'orange', 'banana'); $posArr = array_reduce($arr, function($carry, $item) { static $pos = 0; if($item == 'banana') { $carry[] = $pos; } $pos++; return $carry; }, array()); print_r($posArr); // 输出 Array ( [0] => 1 [1] => 3 )
In the above example, use the array_reduce() function to process the array $arr, and judge each element. If the element is equal to 'banana', record the element position information into the $carry array, and finally returns the $carry array.
It should be noted that this method can easily obtain an array of all element positions that meet the conditions, but compared to the foreach() loop, the time complexity is still high for large arrays. .
To sum up, there are many ways to determine the position of an element in an array in PHP. It is important to choose the appropriate method according to the actual situation.
The above is the detailed content of PHP determines which element of array. For more information, please follow other related articles on the PHP Chinese website!