Home  >  Article  >  Backend Development  >  用php 把数组中偶数,选择出来

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

WBOY
WBOYOriginal
2016-06-23 13:47:53986browse

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

     

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"));

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