array_filter() - 用回调函数过滤数组中的单元 - php 数组函数
array_filter()
(PHP 4 >= 4.0.6, PHP 5, PHP 7)
用回调函数过滤数组中的单元
说明
array_filter(array $array[,callable$callback[,int $flag= 0]]): array依次将$array数组中的每个值传递到$callback函数。如果$callback函数返回 true,则$array数组的当前值会被包含在返回的结果数组中。数组的键名保留不变。
参数
$array要循环的数组
$callback使用的回调函数
如果没有提供$callback函数,将删除$array中所有等值为FALSE
的条目。更多信息见转换为布尔值。
决定$callback接收的参数形式:
ARRAY_FILTER_USE_KEY
-$callback接受键名作为的唯一参数ARRAY_FILTER_USE_BOTH
-$callback同时接受键名和键值
返回值
返回过滤后的数组。
更新日志
版本 | 说明 |
---|---|
5.6.0 | 添加可选的参数$flag,以及常量ARRAY_FILTER_USE_KEY 和ARRAY_FILTER_USE_BOTH 。 |
范例
Example #1array_filter()例子
以上例程会输出:
Odd : Array ( [a] => 1 [c] => 3 [e] => 5 ) Even: Array ( [0] => 6 [2] => 8 [4] => 10 [6] => 12 )
不使用$callback时的array_filter()
以上例程会输出:
Array ( [0] => foo [2] => -1 )
带$flag标记的array_filter()
以上例程会输出:
array(1) { ["b"]=> int(2) } array(2) { ["b"]=> int(2) ["d"]=> int(4) }
注释
Caution用户不应在回调函数中修改数组本身。例如增加/删除单元或者对array_filter()正在作用的数组进行 unset。如果数组改变了,此函数的行为将不可预测。
参见
array_map()
为数组的每个元素应用回调函数array_reduce()
用回调函数迭代地将数组简化为单一的值array_walk()
使用用户自定义函数对数组中的每个元素做回调处理
If you want a quick way to remove NULL, FALSE and Empty Strings (""), but leave values of 0 (zero), you can use the standard php function strlen as the callback function: eg:
Because array_filter() preserves keys, you should consider the resulting array to be an associative array even if the original array had integer keys for there may be holes in your sequence of keys. This means that, for example, json_encode() will convert your result array into an object instead of an array. Call array_values() on the result array to guarantee json_encode() gives you an array.
In case you are interested (like me) in filtering out elements with certain key-names, array_filter won't help you. Instead you can use the following: Result will be something like this: ['element2'] => 2 ['element3'] => 3
Here is how you could easily delete a specific value from an array with array_filter: output: Array ( [0] => 1 [3] => 5 [4] => 6 )
array_filter remove also FALSE and 0. To remove only NULL's use: $af = [1, 0, 2, null, 3, 6, 7]; function is_not_null($val){ return !is_null($val); } $af = array_filter($af, 'is_not_null');
If you want to use array_filter with a class method as the callback, you can use a psuedo type callback like this: Will return even numbers.
Some of PHP's array functions play a prominent role in so called functional programming languages, where they show up under a slightly different name: Functional programming is a paradigm which centers around the side-effect free evaluation of functions. A program execution is a call of a function, which in turn might be defined by many other functions. One idea is to use functions to create special purpose functions from other functions. The array functions mentioned above allow you compose new functions on arrays. E.g. array_sum = array_map("sum", $arr). This leads to a style of programming that looks much like algebra, e.g. the Bird/Meertens formalism. E.g. a mathematician might state map(f o g) = map(f) o map(g) the so called "loop fusion" law. Many functions on arrays can be created by the use of the foldr() function (which works like foldl, but eating up array elements from the right). I can't get into detail here, I just wanted to provide a hint about where this stuff also shows up and the theory behind it.
Here's a function that will filter a multi-demensional array. This filter will return only those items that match the $value given Example: Output : array( 0 => array('key1' => '1', 'key2' => 2, 'key3' => 3) );
This function filters an array and remove all null values recursively. Or with callback parameter (not tested) :
You can access the current key of array by passing a reference to array into callback function and call key() and next() method in the callback function: However be careful with array internal pointer or use reset() method before calling array_filter().
from php 5.3, we use anonymous function as second argument: $a = array('a'=>1, 'b'=>2, 'c'=>false, 'd'=>0); $b = array_filter($a, function($v){return $v !== 0;}); var_dump($b); output: array(3) { 'a' => int(1) 'b' => int(2) 'c' => bool(false) }
To get rid of all white space in an array without looping.
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)