PHP function exploration-array_filter()

WBOY
Release: 2023-06-20 08:02:01
Original
1641 people have browsed it

In PHP programming, arrays are one of the most common and important data types. The function library in PHP also provides numerous array processing functions, one of the important functions is array_filter(). This article will explore the common usage and implementation principles of the array_filter() function.

Basic usage

array_filter() The main function of the function is to filter the elements in the array. When using it, we need to pass in an array as the first parameter, and optionally pass in the second parameter callback function to filter elements. The callback function is an optional parameter. If not passed in, all "false" elements will be filtered out by default, such as null, false, 0,'' wait.

The following is a simple example that demonstrates how to filter an array using the array_filter() function:

// 定义一个示例数组
$numbers = array(100, 2, -3, 4, 5, -6);

// 过滤回调函数,过滤掉小于等于 0 的元素
function positive($n) {
    return $n > 0;
}

// 过滤数组
$filtered = array_filter($numbers, "positive");

// 打印过滤后的数组
print_r($filtered);
Copy after login

In the above example, we define a $numbers Array containing some number elements. We also define a callback function positive(). The function of this function is to return true when the element value is greater than 0, otherwise it returns false. Finally, we call the array_filter() function and pass in the array $numbers and the callback function positive() to filter the array. Running the above example will output the following results:

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

You can see that in the end $filtered only the positive part of the numeric elements is retained in the array, which meets the filtering conditions of the callback function.

In addition to passing in the callback function, the array_filter() function also supports passing in an additional parameter, which will be passed in as the second parameter of the callback function. This can be useful in certain situations, for example, when filtering an array and you need to use the subscript of the array element as a reference condition. Here is an example using the $key parameter:

// 定义一个示例数组
$fruits = array("apple", "banana", "cherry", "date");

// 过滤回调函数,过滤掉下标是偶数的元素
function odd_indices($value, $index) {
    return $index % 2 != 0;
}

// 过滤数组,并保留下标是奇数的元素
$filtered = array_filter($fruits, "odd_indices", ARRAY_FILTER_USE_BOTH);

// 打印过滤后的数组
print_r($filtered);
Copy after login

In the above example, we have defined a $fruits array that contains some fruit elements. We also define a callback function odd_indices(), which will filter out array elements with even indexes. Finally, we passed in the ARRAY_FILTER_USE_BOTH parameter to specify that the callback function requires two parameters. Running the above example will output the following results:

Array
(
    [1] => banana
    [3] => date
)
Copy after login

As you can see, the final $filtered array only retains elements with odd subscripts, while elements with even subscripts are filtered out.

Implementation principle

Understanding how the array_filter() function is implemented will also help us use it better. In PHP implementation, the array_filter() function mainly implements filtering by traversing the array. In the actual process, when we pass in a callback function, the array_filter() function will call the callback function once for each element in the array. If the callback function returns true, it means that this element needs to be retained, otherwise it will be filtered out.

There are also some things to note, such as when the callback function returns a null value, the element will also be filtered out. In addition, if an additional parameter is passed in, the parameter will be passed into the callback function as the second parameter of the callback function.

When we do not pass in the callback function, all "false" elements are filtered out by default (for example: null, false, 0 , empty string, etc.). In the implementation, the array_filter() function converts all elements to boolean values ​​before filtering. If the element is true after conversion, it means that the element is retained; otherwise, the element is filtered out.

Although the implementation of the array_filter() function seems simple, it provides a convenient and powerful way to filter array elements. For PHP developers, being proficient in the use and implementation principles of the array_filter() function will bring a lot of convenience in actual development.

The above is the detailed content of PHP function exploration-array_filter(). 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