How to filter in array php?
(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8) array_filter — Filters elements of an array using a callback function Descriptionarray_filter(array Array keys are preserved, and may result in gaps if the Parametersarray The array to iterate over callback The callback function to use If no mode Flag determining what arguments are sent to
0
which will pass value as the only argument to callback instead. Return ValuesReturns the filtered array. Changelog
ExamplesExample #1 array_filter() example
even($var) The above example will output: Odd : Array ( [a] => 1 [c] => 3 [e] => 5 ) Even: Array ( [0] => 6 [2] => 8 [4] => 10 [6] => 12 ) Example #2 array_filter() without
= [ The above example will output: Array ( [0] => foo [2] => -1 ) Example #3 array_filter() with
= ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4];var_dump(array_filter($arr, function($k) { The above example will output: array(1) { ["b"]=> int(2) } array(2) { ["b"]=> int(2) ["d"]=> int(4) } NotesCaution If the array is changed from the callback function (e.g. element added, deleted or unset) the behavior of this function is undefined. See Also
Anonymous ¶ 9 years ago
nicolaj dot knudsen at gmail dot com ¶ 5 years ago
ASchmidt at Anamera dot net ¶ 1 year ago
TechNyquist ¶ 4 months ago
marc dot vanwoerkom at fernuni-hagen dot de ¶ 18 years ago
marc dot gray at gmail dot com ¶ 8 years ago
Niko E ¶ 11 days ago
justinphiggs at gmail dot com ¶ 2 months ago
$array_count === $filtered_count ) ? true : false; |