What PHP function can be used to filter an array based on specific criteria, such as separating negative and positive numbers?

To filter an array based on specific criteria, such as separating negative and positive numbers, you can use the array_filter() function in PHP. This function allows you to iterate over each element in the array and apply a callback function that returns true or false based on the criteria you specify. In this case, you can create a callback function that checks if the number is negative or positive and return the appropriate boolean value.

// Sample array of numbers
$numbers = [-5, 10, -3, 8, -1, 0, 2];

// Separate negative and positive numbers
$negativeNumbers = array_filter($numbers, function($num) {
    return $num < 0;
});

$positiveNumbers = array_filter($numbers, function($num) {
    return $num > 0;
});

// Output the separated arrays
print_r($negativeNumbers);
print_r($positiveNumbers);