Introduction to PHP functions—array_filter(): Use callback functions to filter elements in an array

WBOY
Release: 2023-07-24 20:44:01
Original
743 people have browsed it

PHP function introduction—array_filter(): Use callback function to filter elements in the array

In PHP programming, array is a very common and important data type. As for array operations, one of the most common operations is to filter elements in the array. PHP provides a very practical function array_filter() to achieve this function.

The array_filter() function is to use the callback function to filter the array, retain the elements in the array that meet the conditions, and return a new array.

The array_filter() function is used as follows:

array array_filter ( array $array [, callable $callback [, int $flag = 0 ]] )
Copy after login

Parameter description:

  • $array: the array to be filtered;
  • $callback : Callback function, used to test elements in the array. This function accepts a parameter, representing the element in the array, and returns a Boolean value in the callback function. If it returns true, the element will be retained, and if it returns false, the element will be filtered. If the callback function is not provided, elements with a value of false are filtered out by default;
  • $flag: optional parameter, which specifies the number of parameters passed in the callback function. The default is 0, which means the callback function only accepts one parameter (an element in the array). If a value greater than 1 is passed, the callback function accepts two parameters, the first parameter is the element in the array, and the second parameter is the key of the element.

Below we use several examples to demonstrate the use of the array_filter() function.

Example 1: Filter even numbers in the array

Copy after login

Run the above code, the output result is:

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

Example 2: Filter empty values ​​in the associative array

 'Tom', 'age' => '', 'gender' => 'male', 'email' => ''];

// 过滤函数
function filter_empty($value) {
    return ($value !== '');
}

// 使用array_filter()函数
$new_arr = array_filter($arr, 'filter_empty');

// 输出结果
print_r($new_arr);
?>
Copy after login

Run the above code, the output result is:

Array
(
    [name] => Tom
    [gender] => male
)
Copy after login

The above two examples respectively show two cases of using the array_filter() function to filter elements in the array. In actual development, the array_filter() function has many other application scenarios, which we can use flexibly according to actual needs.

Summary:
The array_filter() function is a very commonly used and practical function in PHP. It provides a convenient way to filter elements in an array. By using callback functions to determine whether array elements meet conditions, we can implement various flexible filtering requirements. I hope the introduction in this article can help everyone and give everyone a deeper understanding of the use of the array_filter() function.

The above is the detailed content of Introduction to PHP functions—array_filter(): Use callback functions 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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!