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 a 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 particular 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

Connect With CodexWorld