用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 :
";
print_r(array_filter($array1, "odd"));
echo "Even:
";
print_r(array_filter($array2, "even"));

原文地址:https://www.cnblogs.com/mfrbuaa/p/5170832.html