Home>Article>Backend Development> How to use array_filter function in PHP to filter arrays based on callback function
In web-based development, PHP is a widely used language because of its simplicity and flexibility. PHP can handle a wide variety of tasks, including manipulating arrays and filtering array elements. In PHP, there are several functions that can filter array elements, one of the most powerful functions is thearray_filter
function. This function is very useful because it can filter the elements of the array based on a specific callback function and only retain elements that meet the conditions. Next, we will discuss in detail how to use thearray_filter
function in PHP to filter arrays.
array_filter
The syntax for using the function is as follows:
array_filter ( array $array [, callable $callback [, int $flag = 0 ]] ) : array
The first parameter: the array to be filtered.
Second parameter (optional): callback function, used to test array elements according to your own conditions. If this parameter is not specified, only elements equivalent to FALSE are removed.
The third parameter (optional): flag, used to set the behavior of the callback function. The following flags can be selected:
Note:array_filter
The function does not modify the original array, but returns a new filtered array.
Next, we will show how to use thearray_filter
function through some examples.
2 && $value < 6); } $a = array(1, 2, 3, 4, 5, 6); $b = array_filter($a, "testFilter"); var_dump($b); ?>
The running result is:
array(3) { [2]=> int(3) [3]=> int(4) [4]=> int(5) }
In this example, we define atestFilter
callback function, which will test the array elements Whether it is between 2 and 6 (excluding 2 and 6). We then pass this function to thearray_filter
function with the array we need to process as the first argument. The new array$b
only contains elements that meet the conditions, that is, 3, 4, and 5. In this example we use a filter callback function, the function name is passed as a string to thearray_filter
function.
2 && $value > 2); } $a = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5); $b = array_filter($a, "testFilter2", ARRAY_FILTER_USE_BOTH); var_dump($b); ?>
The running result is:
array(3) { ["c"]=> int(3) ["d"]=> int(4) ["e"]=> int(5) }
In this example, we defined atestFilter2
callback function and usedARRAY_FILTER_USE_BOTH
Flag passes the key name and value parameters to the callback function. In this example, the callback function will only return elements whose key name and value are greater than 2. The filtered array returned in this example contains only "c", "d" and "e" elements.
The running result is:
array(3) { [0]=> string(1) "a" [2]=> string(1) "b" [4]=> string(1) "c" }
In this example, we define atestFilter3
callback function, which is used to determine the value based on the value. Whether equivalent to empty string, NULL or FALSE to filter array elements. In this example, the new array$b
only contains elements from the original array that are not empty, NULL, or FALSE.
Summary:
array_filter
function is a very useful function in PHP array processing function, which can be used to filter existing arrays to create an array checked by conditions An array composed of the following elements. In this article, we have provided examples for different use cases that can help you better understand the function and its usage, which will help you filter arrays faster and categorize array elements in PHP.
The above is the detailed content of How to use array_filter function in PHP to filter arrays based on callback function. For more information, please follow other related articles on the PHP Chinese website!