Skip to main content

Posts

Showing posts from February, 2018

Filter Array by a condition 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 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. $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 ...