PHP array_filter() function filters elements of an array using a callback function and returns the filtered array. Here we’ll provide a PHP code 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 a multidimensional array by value using array_filter() functions in PHP.
And here is another example code will filter the elements of a an array by value using array_filter() and strpos() functions in PHP.
The following example code will filter the elements of a multidimensional array by value using array_filter() functions in PHP.
$newArr = array( array( 'name' => 'Fine', 'amount' => 1000 ) , array( 'name' => 'Transport Fee', 'amount' => 0 ) , array( 'name' => 'Cheque Bounce', 'amount' => 50 ) , array( 'name' => 'TC', 'amount' => 100 ) ); $filterArray = array_filter($newArr, function ($var) { return ($var['amount'] != 0); }); // Output the filtered array print_r($filterArray);
//OUTPUT
Array(
[0] => Array(
[name] => Fine
[amount] => 1000
) [2] => Array(
[name] => ChequeBounce
[amount] => 50
) [3] => Array(
[name] => TC
[amount] => 100
)
)
And here is another example code will filter the elements of a 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);
//OUTPUT
Array(
[month2] => February
[month3] => March
)
Comments
Post a Comment