Home >Backend Development >PHP Tutorial >《PHP-运用回调函数实现数组的筛选》

《PHP-运用回调函数实现数组的筛选》

WBOY
WBOYOriginal
2016-06-13 10:52:24908browse

《PHP---使用回调函数实现数组的筛选》

<div style="color:red">
<?php //如果是奇数则返回function odd($var){   return($var % 2 == 1);}//如果是偶数则返回function even($var){   return($var % 2 == 0);}$nums1 = array("111"=>1, "222"=>2, "333"=>3, "444"=>4, "555"=>5);$nums2 = $nums1;echo "<pre class="brush:php;toolbar:false">";echo "Odd :\n";//回调函数,如果返回true则将$array1中的数据再赋给原数组,键保持不变print_r(array_filter($nums1, "odd"));echo "
";echo "
";echo "Even:\n";print_r(array_filter($nums2, "even"));echo "
";?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:《PHP-自定义创建目录资料方法》-如果目录已存在,该目录必须为空才可继续创建Next article:急需大家帮忙了 所有人都来看下需要找一个

Related articles

See more