php - Searching Multi-Dimension Array With Multiple Results -
let's have array , want search value should return multiple results:
array(2) { [0] => array(2) { ["id"] => string(2) "1" ["custom"] => string(1) "2" } [1] => array(2) { ["id"] => string(2) "2" ["custom"] => string(1) "5" } [2] => array(2) { ["id"] => string(2) "3" ["custom"] => string(1) "2" } }
i want search key custom
value = 2
, following result:
array(2) { [0] => array(2) { ["id"] => string(2) "1" ["custom"] => string(1) "2" } [1] => array(2) { ["id"] => string(2) "3" ["custom"] => string(1) "2" } }
is possible without looping through array? there such class or built in function this?
the array_filter
function want.
$array = [ [ 'id' => '1', 'custom' => '2' ], [ 'id' => '2', 'custom' => '5' ], [ 'id' => '3', 'custom' => '2' ] ]; $customs = array_filter( $array, function ($arr) { return is_array($arr) && array_key_exists('custom', $arr) && $arr['custom'] === '2'; } );
Comments
Post a Comment