Home > Backend Development > PHP Tutorial > The array_filter() function in PHP is used to filter elements in an array

The array_filter() function in PHP is used to filter elements in an array

王林
Release: 2023-11-18 08:46:02
Original
1584 people have browsed it

The array_filter() function in PHP is used to filter elements in an array

The array_filter() function in PHP is used to filter elements in an array. It can filter the array according to the specified callback function and return a new filtered array. This article will introduce the usage of array_filter() function and provide specific code examples.

The syntax of the array_filter() function is as follows:
array_filter(array $input, callable $callback = null, int $flag = 0): array

Parameter description:

  • $input: required. The input array.
  • $callback: optional. The specified callback function.
  • $flag: optional. Specify the behavior flags of the callback function. The default value is 0, which means that the parameter of the callback function is an array element; if it is 1, it means that the parameter is an array index; if it is 2, it means that the parameters are also passed to the callback function.

Function return value:
Returns the new filtered array.

Code example 1: Filter even numbers in the array

<?php
$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

// 使用匿名函数过滤偶数
$filtered_numbers = array_filter($numbers, function($value) {
    return ($value % 2) == 0;
});

print_r($filtered_numbers);
?>
Copy after login

Output result:

Array
(
    [1] => 2
    [3] => 4
    [5] => 6
    [7] => 8
    [9] => 10
)
Copy after login

Code example 2: Filter empty strings in the array

<?php
$names = ["Alice", "", "Bob", " ", "Carol"];

// 使用内置函数trim()过滤掉空字符串
$filtered_names = array_filter($names, 'trim');

print_r($filtered_names);
?>
Copy after login

Output result:

Array
(
    [0] => Alice
    [2] => Bob
    [3] =>
    [4] => Carol
)
Copy after login

As can be seen from the above example, the array_filter() function is very flexible. In actual development, you can write a callback function according to your own needs to perform various filtering operations on the array. At the same time, we can also implement more complex filtering logic by setting the parameters of the callback function.

Note: When using the array_filter() function, pay attention to the return value of the callback function. Return true to retain the element, return false to filter out the element.

To sum up, the array_filter() function is a very useful array filtering function in PHP. It can filter the elements in the array according to the specified callback function and return the filtered new array. By rationally using the array_filter() function, the development process can be simplified and the readability and maintainability of the code can be improved.

The above is the detailed content of The array_filter() function in PHP is used to filter elements in an array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template