009——数组(九) each list array_map array_walk array_walk_recursive

<?php
/**
 * 9 数组 each list array_map array_walk array_walk_recursive
 */

//each() 返回数组中的键名和键值生成新数组,如果指针结束,返回布尔值false
/*
$array1=array('webname'=>"博客",'weburl'=>"bbs.blog.com");
$array_new=each($array1); //生成新的数组:
print_r($array_new);
//输出:Array ( [1] => 博客 [value] => 博客 [0] => webname [key] => webname )
$array_new=each($array1); //生成新的数组:
print_r($array_new);
//输出:Array ( [1] => bbs.blog.com [value] => bbs.blog.com [0] => weburl [key] => weburl )
*/

//list() 将索引数组的值,赋给变量
/*$array1=array("博客","bbs.blog.com");
list($value)=$array1;
echo $value;//输出:博客*/

/*$array1=array('webname'=>"博客",'weburl'=>"bbs.blog.com");
while (list($k,$v)=each($array1)){
    echo "键名:".$k."=>"."键值".$v;
}//输出:键名:webname=>键值博客键名:weburl=>键值bbs.blog.com*/

//array_map() 数组的每一个元素应用,都经过回调函数处理,返回值是处理过的元素组成的新数组
/*$blog=array(11,22,33,440,55,660);
function func_map($v){
   echo $v; //输出:11223344055660
}
$array_new=array_map('func_map',$blog);*/

/*$blog=array(11,22,33,440,55,660);
function func_map($v){
    if ($v>100){
        return $v;
    }
}
$array_new=array_map('func_map',$blog);
print_r($array_new);//输出:Array ( [0] => [1] => [2] => [3] => 440 [4] => [5] => 660 )*/

/*$blog=array(11,22,33,440,55,660);
function func_map($v){
    return $v+10;
}
$array_new=array_map('func_map',$blog);
print_r($array_new);//输出:Array ( [0] => 21 [1] => 32 [2] => 43 [3] => 450 [4] => 65 [5] => 670 )*/

/*$arr1=array(1,2,3,4,5);
$arr2=array("a","b","c","d","e");
function func4($arr1,$arr2){
    //echo $arr1;//输出:12345
    return array($arr2=>$arr1);
}

$arr0=array_map(func4,$arr1,$arr2);
print_r($arr0);
//输出:Array ( [0] => Array ( [a] => 1 ) [1] => Array ( [b] => 2 ) [2] => Array ( [c] => 3 ) [3] => Array ( [d] => 4 ) [4] => Array ( [e] => 5 ) )*/


//array_walk()  数组中的每一个元素,应用回调函数,成功返回true,失败返回false
/*$array1=array('张三'=>1000,'李四'=>900,'王五'=>3000,'赵六'=>400);
function func_walk(&$v,$k){
    if ($v<1000){
        $v+=500;
    }
}
if (array_walk($array1,func_walk)){
    echo "加工资成功:";
    print_r($array1);
    //输出:加工资成功:Array ( [张三] => 1000 [李四] => 1400 [王五] => 3000 [赵六] => 900 )
}*/

/*$array1=array('张三'=>1000,'李四'=>900,'王五'=>3000,'赵六'=>400);
function func_walk(&$v,$k,$c=""){
    if ($v<1000){
        $v+=$c;
    }
}
if (array_walk($array1,func_walk,1000)){
    echo "加工资成功:";
    print_r($array1);
    //输出:加工资成功:Array ( [张三] => 1000 [李四] => 1900 [王五] => 3000 [赵六] => 1400 )
}*/

//array_walk_recursive() 递归的操作数组,使数组中的元素应用回调函数
$arr5 = array(
    array('uname' => '李四', 'money' => 100),
    array('uname' => '张三', 'money' => 600),
    array('uname' => '王五', 'money' => 700),
    array('uname' => '赵六', 'money' => 300)
);

function fun5(&$v,$k){
    //echo $v; //输出:李四500张三600王五700赵六800
    if ($k=='money' && $v<300){
        $v+=1000;
    }
}
array_walk_recursive($arr5,'fun5');
print_r($arr5);//输出:Array ( [0] => Array ( [uname] => 李四 [money] => 1100 ) [1] => Array ( [uname] => 张三 [money] => 600 ) [2] => Array ( [uname] => 王五 [money] => 700 ) [3] => Array ( [uname] => 赵六 [money] => 300 ) )

  

posted @ 2017-11-15 07:00  生如逆旅,一苇以航  阅读(194)  评论(0编辑  收藏  举报