When processing data returned by the API, we often need to extract valid information from it while excluding some useless or invalid items. PHP provides a very practical function array_filter() , which can be used to filter elements in an array and remove unwanted items based on specific conditions. In this article, we will learn how to use the array_filter() function to filter the API to return invalid items in the data.
The array_filter() function is used to filter elements in an array. This function will determine whether each element is retained based on the given callback function. If the callback function returns true , the element is retained; if false , the element is removed.
array_filter(array $array, callable $callback = null, int $mode = 0): array
$array : an array to be filtered.
$callback : A callback function for filtering. If not provided, the element with a value of false is removed.
$mode : Determines the returned array index method. It can be ARRAY_FILTER_USE_KEY or ARRAY_FILTER_USE_BOTH .
Suppose we get a JSON format data through the API and convert it into a PHP array. Next, we need to remove invalid items in the array, such as items with empty value, null value, or false value.
Suppose we have the data returned by the following API:
$response = [
'id' => 1,
'name' => 'John Doe',
'email' => 'john.doe@m66.net',
'phone' => '',
'address' => null,
'isActive' => false
];
In this data, some fields such as phone and address are invalid (values are empty or null ), and we need to remove them.
We can filter these invalid items through the array_filter() function. The following is the implementation code:
<?php
// API Returned data
$response = [
'id' => 1,
'name' => 'John Doe',
'email' => 'john.doe@m66.net',
'phone' => '',
'address' => null,
'isActive' => false
];
// use array_filter Invalid filtering
$filteredData = array_filter($response, function($value) {
return !empty($value); // Filter out empty values、null、false wait
});
// Print filtered data
print_r($filteredData);
?>
After executing the above code, the output $filteredData will only contain valid items:
Array
(
[id] => 1
[name] => John Doe
[email] => john.doe@m66.net
)
As you can see, phone , address and isActive were successfully removed.
In addition to using empty() to filter empty values, you can also customize filtering conditions according to actual needs. For example, if you only want to remove items with false value and leave empty strings and null , you can adjust the callback function as follows:
$filteredData = array_filter($response, function($value) {
return $value !== false; // Remove only the value as false Items of
});
Through the array_filter() function, PHP provides an easy way to filter the API to return invalid items in the data. You can define filtering conditions based on your needs, such as removing null values, null or specific invalid values. After using array_filter() , the remaining data will be more concise and effective, suitable for further processing or display.