How to Filter Array by Value in PHP

PHP array_filter() function filters elements of an array using a callback function and returns the filtered array. Here we’ll provide short PHP code snippets to filter elements of an array that contain a specific value. It will help you to filter an array based on the specific condition.

The following example code will filter the elements of an array by value using array_filter() and strpos() functions in PHP.

$array = array(
    
'month' => 'January',
    
'month2' => 'February',
    
'month3' => 'March'
);

$filterArray array_filter($array, function ($var) {
    return (
strpos($var'Jan') === false);
});

// Output the filtered array
print_r($filterArray);

Leave a reply

keyboard_double_arrow_up