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

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

WBOY
Release: 2016-06-13 10:52:24
Original
903 people have browsed it

《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 "
Copy after login
";echo "
";echo "Even:\n";print_r(array_filter($nums2, "even"));echo "
Copy after login
";?>

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template