<?php //过滤奇数与偶数 function odd($var) { return($var & 1);//按位与 } function even($var) { 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")); //修改key为a的元素的值 function myfunction(&$value,$key) { if($key=='a'){ $value="yellow"; } } $a=array("a"=>"red","b"=>"green","c"=>"blue"); array_walk($a,"myfunction"); print_r($a); //array_map()的应用 $arr1 = array(1,2,3,4,5,6); $arr2 = array(7,8,9,10,11,12); function a($val,$val2){ return $val*$val2; } $new_arr = array_map('a',$arr1, $arr2); var_dump($new_arr); ?>
按位与知识
十进制 3换算成二进制 00000011
十进制 1换算成二进制 00000001
按位& 00000001 // 就是各个位数相同的不变否则都算成0