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
Post a Comment