4

Suppose I have a php array like this:

$shop = array( array("name"=>"Tom", "level"=> 1.25 ),
               array("name"=>"Mike","level"=> 0.75 ),
               array("name"=>"John","level"=> 1.15 ) 
             ); 

I want to filter this array similar to filtering a mysql table with where conditions. Supposedly I want every array where level is higher than 1. I could iterate through and check with if statements. Are there any php solutions to this?

Borut Flis
  • 15,715
  • 30
  • 92
  • 119
  • This might Interest you : http://stackoverflow.com/questions/14972025/implementing-mongodb-like-query-expression-object-evaluation – Baba Mar 26 '13 at 20:00

1 Answers1

7

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
    )
)
tuffkid
  • 1,323
  • 9
  • 8