用php 把数组中偶数,选择出来

WBOY
Release: 2016-06-23 13:47:53
Original
1029 people have browsed it

我有这样的一个小算法,把数组中的所有的偶数或技术分别选择出来。很多人可能,会循环这个数组,而我恰恰不循环数组就能做到这一点,代码如下。

     

function odd($var){    // returns whether the input integer is odd    return($var & 1);}function even($var){    // returns whether the input integer is even    return(!($var & 1));}$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);$array2 = array(6, 7, 8, 9, 10, 11, 12);echo "Odd :\n";print_r(array_filter($array1, "odd"));echo "Even:\n";print_r(array_filter($array2, "even"));
Copy after login

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template