Blogger Information
Blog 22
fans 0
comment 0
visits 17800
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
array_filter()、array_walk()、array_map()的应用
yestrue的博客
Original
755 people have browsed it
<?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

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post