Home > Article > Backend Development > Array functions in PHP8: various practical applications of array_search()
PHP is a programming language widely used in Web development. The various new features added in its latest version, PHP8, have attracted much attention from developers. One of the important features is the improvement of array functions, of which the array_search() function is one of the most noteworthy. In this article, we will introduce various practical applications of the array_search() function so that everyone can better master this practical function.
1. What is the array_search() function?
Before understanding the various applications of the array_search() function, we need to first understand what this function is and what it is used for. Simply put, the array_search() function is used to find the index position of the specified element in the array. If it is found, it returns the key of the element in the array, otherwise it returns false.
This function has the following basic syntax:
array_search($search, $array, $strict)
Among them, $search is the element to be found, $array is the array to be found, $strict is an optional parameter, used to control whether Do "strict mode" (i.e. parameter types must match exactly). There are two results returned by the function: if the search is successful, the key of the element in the array is returned, otherwise false is returned.
Next, we will introduce a variety of practical application scenarios so that everyone can better understand the array_search() function.
2. Delete the specified element of the array
Sometimes, we need to delete the specified element from an array. In this case, we can use the array_search() function to find the index position of the element to be removed and then use the array_splice() function to remove it from the array.
The sample code is as follows:
$arr = array('a', 'b', 'c', 'd', 'e'); $search = 'c'; $key = array_search($search, $arr); if ($key !== false) { // 判断是否查找到了指定元素 array_splice($arr, $key, 1); } print_r($arr);
After executing the above code, the output result is as follows:
Array ( [0] => a [1] => b [2] => d [3] => e )
As you can see, element 'c' in the array has been deleted. This method is suitable for situations where you need to "quickly delete" an array element, because it operates directly inside the array and does not require copying the entire array.
3. Check whether the array contains the specified element
During the development process, we sometimes need to check whether an array contains the specified element. The array_search() function can help us accomplish this task. Here is an example:
$arr = array('apple', 'banana', 'pear', 'grape'); $search = 'apple'; $key = array_search($search, $arr); if ($key !== false) { echo 'The fruit "' . $search . '" exists in the array.' . '<br>'; } else { echo 'The fruit "' . $search . '" does not exist in the array.' . '<br>'; }
Here, we first define an array containing the names of four fruits. Then, we define a $search variable and use the array_search() function to find the element in the array. Finally, we use an if statement to check the value of the $search variable, if it is not false, it means that the element is present in the array.
4. Search for matching items in a two-dimensional array
The array_search() function is very useful when searching a one-dimensional array, but it is slightly more complicated when searching a two-dimensional array. In this case, we can use a foreach loop to search each element of the array, and then use the array_search() function to find the specified element.
The following is a sample code involving a two-dimensional array:
$array = array( array('name' => 'Alice', 'age' => 22), array('name' => 'Bob', 'age' => 23), array('name' => 'Charlie', 'age' => 24) ); $search = 'Bob'; foreach ($array as $key => $value) { if (array_search($search, $value)) { echo 'The person named ' . $search . ' exists in the array.' . '<br>'; } else { echo 'The person named ' . $search . ' does not exist in the array.' . '<br>'; } }
Here, we define a two-dimensional array containing the names of three people ('Alice', 'Bob' and 'Charlie') and their ages. We then use a foreach loop to search for each element in the array. In the loop, we use the array_search() function to find the name of the specified person and if a match is found, output a message on the web page, otherwise output another message.
5. Search for matching items in multiple arrays
If we need to search for matching items in multiple arrays, the array_search() function can also help us complete this task. Here is an example:
$array1 = array('a', 'b', 'c'); $array2 = array('d', 'e', 'f'); $array3 = array('g', 'h', 'i'); $search = 'c'; $found = false; foreach (array($array1, $array2, $array3) as $array) { if (array_search($search, $array)) { $found = true; break; } } if ($found) { echo 'The element "' . $search . '" exists in one of the arrays.' . '<br>'; } else { echo 'The element "' . $search . '" does not exist in any of the arrays.' . '<br>'; }
Here, we define three arrays $array1, $array2 and $array3, and define a $search variable. Then, we use a foreach loop to iterate through all arrays, using the array_search() function to find the specified element in each array. If a match is found, we set a $found variable to true and then use the break statement to break out of the loop. Finally, we use an if statement to check the value of the $found variable to determine if the specified element is found in any array.
6. Search for paths in multi-dimensional arrays
In some cases, we need to search for a specified path in a multi-dimensional array and return the elements in the path. Here is a use case:
$array = array( 'a' => array('b' => array('c' => 'd')) ); $search = 'c'; function search_path($array, $search, &$path = array()) { foreach ($array as $key => $value) { if (is_array($value)) { $path[] = $key; if (search_path($value, $search, $path)) { return true; } array_pop($path); } else { if ($key === $search) { $path[] = $key; return true; } } } return false; } $path = array(); if (search_path($array, $search, $path)) { echo 'The path to "' . $search . '" is: ' . implode(' > ', $path) . '<br>'; } else { echo 'Could not find the path to "' . $search . '".' . '<br>'; }
In this example, we define a multidimensional array containing a scalar value 'd', saved at the path 'a/b/c'. We then use a custom function search_path() to find the path and return the elements on the path. This function uses recursion to traverse the multi-dimensional array and find elements in the specified path. If found, store the path in a $paths array and return true. If not found, the function returns false.
After executing the search_path() function, we pass the $path array as a parameter and can use the implode() function to display the path on the web page.
Conclusion
In this article, we introduced various practical application methods of the array_search() function. This function can not only find elements in one-dimensional arrays, but also find paths in multi-dimensional arrays. Mastering this function can make us operate arrays and data more conveniently and efficiently during the development process.
The above is the detailed content of Array functions in PHP8: various practical applications of array_search(). For more information, please follow other related articles on the PHP Chinese website!