php - How to filter an array by a condition -


i have array this:

array("a" => 2, "b" => 4, "c" => 2, "d" => 5, "e" => 6, "f" => 2) 

now want filter array condition , keep elements value equal 2 , delete elements value not 2.

so expected result array be:

array("a" => 2, "c" => 2, "f" => 2) 

note: want keep keys original array.

how can php? built-in functions?

$fullarray = array('a'=>2,'b'=>4,'c'=>2,'d'=>5,'e'=>6,'f'=>2);   function filterarray($value){     return ($value == 2); }  $filteredarray = array_filter($fullarray, 'filterarray');  foreach($filteredarray $k => $v){     echo "$k = $v"; } 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -