Where similar requests on multidimensional arrays in php
Suppose I have a php array:
$shop = array( array("name"=>"Tom", "level"=> 1.25 ), array("name"=>"Mike","level"=> 0.75 ), array("name"=>"John","level"=> 1.15 ) ); I want to filter out this array, similar to filtering a mysql table with conditions. Presumably, I want every array where the level is higher than 1. I could iterate and check using if statements. Are there any php solutions?
array_filter is what you are looking for:
$results= array_filter($shop, function($item) { return $item['level'] > 1; }); print_r($results); Output:
Array ( [0] => Array ( [name] => Tom [level] => 1.25 ) [2] => Array ( [name] => John [level] => 1.15 ) )