热搜:NVER node 开发 php

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

2024-08-20 13:40:01
用php 把数组中偶数,选择出来

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

     

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