In PHP, it is very common to use two-dimensional arrays. However, when we need to query certain values in a two-dimensional array, how can we easily perform fuzzy queries? This article will introduce how to implement two-dimensional array fuzzy query in PHP.
1. What is a two-dimensional array
First, let’s understand what a two-dimensional array is. Simply put, a two-dimensional array is one or more arrays nested within an array. Each array can have its own index or key-value pair. The following is an example of a simple two-dimensional array:
//定义一个关联型二维数组 $student = array( array('name' => '小明', 'age' => 18, 'gender' => '男'), array('name' => '小红', 'age' => 17, 'gender' => '女'), array('name' => '小刚', 'age' => 19, 'gender' => '男') );
2. Implement two-dimensional array fuzzy query
Now, we have a two-dimensional array, assuming we need to query all ages less than or equal to For an 18-year-old student, it must be very troublesome to directly traverse the entire array. At this time, we can use PHP's array_filter() function to perform fuzzy queries.
The array_filter() function is to apply the specified callback function to each element in the array, and the elements whose return result is true form a new array. We can use the callback function to determine whether the current element meets the conditions. The following is an example of using the array_filter() function to implement a two-dimensional array fuzzy query:
function filterByAge($arr) { return ($arr['age'] <= 18); } $result = array_filter($student, 'filterByAge'); print_r($result);
In this example, we define a callback function named filterByAge, which will return when the student's age is less than or equal to 18 Student information at age. Finally, we use the array_filter() function to filter out the student information that meets the conditions in the two-dimensional array, and then output the results to the screen.
3. Optimize two-dimensional array fuzzy query
Although the above example shows how to use the array_filter() function to perform fuzzy query, when the amount of data is large, this method will become very slow. We can optimize in another way:
$search_age = 18; $result = array_filter($student, function ($arr) use ($search_age) { return ($arr['age'] <= $search_age); }); print_r($result);
In this example, we use an anonymous function instead of a function named filterByAge. In addition, we also used the use keyword to pass the $search_age variable to the anonymous function. Although this may be slightly more complex, this approach can speed up your queries.
4. Summary
In PHP, the use of two-dimensional arrays is very common. When we need to query qualified information in a two-dimensional array, we can use the array_filter() function to perform fuzzy query. Using anonymous functions and the use keyword can improve query speed, but the code may be slightly more complex. Hope this article can help you understand how to implement two-dimensional array fuzzy query in PHP.
The above is the detailed content of How to implement fuzzy query of two-dimensional array in PHP. For more information, please follow other related articles on the PHP Chinese website!