zoukankan      html  css  js  c++  java
  • 用回调函数对数组中的每个元素自定义【array_walk】

    <?php
    $array = array(
    0 => '霜天部落',
    1 => false,
    2 => 1,
    3 => null,
    4 => '',
    5 => 'http://www.XXX.com',
    6 => '0'
    );
    function myFun($value,$key){
    if($key %2 == 0){
    echo "The key $key has the value $value<br/>";
    }
    }
    array_walk($array,'myFun');
    print_r($array);
    /*
    输出:
    The key 0 has the value 霜天部落
    The key 2 has the value 1
    The key 4 has the value
    The key 6 has the value 0
    Array ( [0] => 霜天部落 [1] => [2] => 1 [3] => [4] => [5] => http://www.XXX.com [6] => 0 )
    */


    ?>

    补充:

    <?php
    $array = array(
    0 => '霜天部落',
    1 => false,
    2 => 1,
    3 => null,
    4 => '',
    5 => 'http://www.XXX.com',
    6 => '0'
    );
    function myFun(&$value,$key){//传入引用
    $value = '好的';
    if($key %2 == 0){
    echo "The key $key has the value $value<br/>";
    }
    }
    array_walk($array,'myFun');
    print_r($array);
    /*
    输出:
    The key 0 has the value 好的
    The key 2 has the value 好的
    The key 4 has the value 好的
    The key 6 has the value 好的
    Array ( [0] => 好的 [1] => 好的 [2] => 好的 [3] => 好的 [4] => 好的 [5] => 好的 [6] => 好的 )
    */


    ?>

  • 相关阅读:
    uploadify
    mark down pad2
    yii1.1.3主从(多从)、读写分离配置
    yii多数据库
    Uploadify上传问题
    出现upstream sent too big header while reading response header from upstream错误
    Nginx 启动脚本/重启脚本
    VB6_小林的气象类模块
    进程与线程
    JDK动态代理与CGLib
  • 原文地址:https://www.cnblogs.com/lbs8/p/5722406.html
Copyright © 2011-2022 走看看