wordpress CMS主题 微信
Home > 网站建设 > PHP array (two): the filter function

PHP array (two): the filter function

高时银博客 网站建设 点击: 1,772 次 0 0

The filter function array_filler (PHP) in array applications will often encounter, thefunction of a callback function to filter the elements in the array, if the customfilter function returns true, the values in the array are operation will be included in the returned results in an array, and the results form a new array. If the originalarray is an associative array, keys are preserved. Below, we have a look togetherhow to use this filter function.

Syntax structure: array_filler (array, function) where array is required, providedthe input arrays. Function must, name of the custom function. The followingexample:

function odd($v){return ($v%2==0);

}

$a=array("one"=>"6",3,"two"=>3,3,22,98);

print_r(array_filter($a,"odd"));

Results: Array ([one] = 6 [3] = 22 [4] = > 98) in 6 and 22, 98 divided by 2 is 0.

If the function in the application does not call the callback function parameters,then, the function will remove the array of all equivalent to the elements of the false, as the following example:

function odd($v){return ($v%2==0);

}

$a=array("one"=>"6",-1,"two"=>false,"hao",null,22,0);

print_r(array_filter($a));

Results: Array ([one] = 6 [0] = > -1 [1] = > Hao [3] = > 22) the original array of false, null, 0 are equivalent to false, so these are removed filter.


文章作者:高时银博客
本文地址:http://wanlimm.com/77201404041847.html
版权所有 © 转载时必须以链接形式注明作者和原始出处!

目前还没有评论。赶快来坐沙发吧。

发表评论