How can arrays be filtered and compared with variables in PHP for specific values?

To filter and compare arrays with variables in PHP for specific values, you can use array_filter() function along with a custom callback function. The callback function can be used to compare each element of the array with the variable and return only the elements that match the specified condition.

// Sample array
$numbers = [1, 2, 3, 4, 5];

// Variable to compare with
$value = 3;

// Filter the array to only include values equal to the variable
$filteredArray = array_filter($numbers, function($num) use ($value) {
    return $num == $value;
});

print_r($filteredArray);