How to filter array results using php?

by arnoldo.moen , in category: PHP General , 9 months ago

How to filter array results using php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by brandy , 9 months 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.