How to filter array results using php?

by arnoldo.moen , in category: PHP General , a year ago

How to filter array results using php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by brandy , a year ago

@arnoldo.moen 

To filter array results in PHP, you can use the array_filter() function. Here's an example:

1
2
3
4
5
6
$numbers = [1, 2, 3, 4, 5];
$filteredNumbers = array_filter($numbers, function($number) {
    return $number > 3;
});

print_r($filteredNumbers);


In this example, the array_filter() function is used to filter out numbers that are greater than 3 from the original array. The function passed to array_filter() returns true for elements that we want to keep and false for elements that we want to filter out.


You can also use the array_filter() function with a custom callback function to create more complex filtering conditions.

Related Threads:

How to filter zero in array using php?
How to filter zero in php array?
How to filter results in graphql?
How to filter search results in laravel?
How to filter dbpedia results in sparql?
How to remove duplicates in array using PHP?