Home  >  Article  >  Backend Development  >  How to use array_filter function in PHP to filter arrays based on callback function

How to use array_filter function in PHP to filter arrays based on callback function

PHPz
PHPzOriginal
2023-06-26 13:34:03837browse

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 the array_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 the array_filter function in PHP to filter arrays.

array_filterThe 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:

  • ARRAY_FILTER_USE_KEY - Pass the key name instead of the value to the callback function.
  • ARRAY_FILTER_USE_BOTH - Pass the value and key name to the callback function.

Note: array_filterThe function does not modify the original array, but returns a new filtered array.

Next, we will show how to use the array_filter function through some examples.

Example 1

<?php
function testFilter($value) {
    return($value > 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 a testFilter 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 the array_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 the array_filter function.

Example 2

<?php
function testFilter2($key, $value) {
    return($key > 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 a testFilter2 callback function and used ARRAY_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.

Example 3

<?php
function testFilter3($value) {
    return $value !== '' && $value !== null && $value !== false;
}

$a = array("a", "", "b", null, "c", false);
$b = array_filter($a, "testFilter3");

var_dump($b);
?>

The running result is:

array(3) {
  [0]=>
  string(1) "a"
  [2]=>
  string(1) "b"
  [4]=>
  string(1) "c"
}

In this example, we define a testFilter3 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn