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] => 好的 )
    */


    ?>

  • 相关阅读:
    用RBG颜色设置自定义颜色
    Swift UI
    SVN上传代码时代码失败
    coredata中谓词的使用
    Core Data的使用(二)备
    CoreData (四)备
    CoreData (三)备
    CI框架篇之模型篇--直接操作(2)
    CI框架篇之模型篇--初识(1)
    CI框架篇之视图篇--载入(1)
  • 原文地址:https://www.cnblogs.com/lbs8/p/5722406.html
Copyright © 2011-2022 走看看