zoukankan      html  css  js  c++  java
  • PHP array_map() 函数详解

    定义

    array_map - 对给定的诸多数组使用回调函数


    描述

    array_map ( callable $callback , array $array1 [, array $... ] ) : array
    

    将传入的数组按对应值传递给回调函数,回调函数处理结果组成新的数组作为返回值返回;
    传入的数组个数必须与回调函数的形参个数相同。


    示例一

    <?php
    function show_Spanish($n, $m)
    {
        return "The number {$n} is called {$m} in Spanish";
    }
    
    $a = [1, 2, 3];
    $b = ['uno', 'dos', 'tres'];
    
    $c = array_map('show_Spanish', $a, $b);
    print_r($c);
    ?>
    

    将输出:

    Array
    (
        [0] => The number 1 is called uno in Spanish
        [1] => The number 2 is called dos in Spanish
        [2] => The number 3 is called tres in Spanish
    )
    

    示例二

    如果传入多个数组,每个数组的长度必须相同,否组,短的数组会用空值扩充成对应长度。
    这个函数可用于构建一个多维数组( zip operation of arrays ),像下面这样

    <?php
    $a = [1, 2, 3];
    $b = ['one', 'two', 'three'];
    $c = ['uno', 'dos', 'tres'];
    
    $d = array_map(null, $a, $b, $c);
    print_r($d);
    ?>
    

    将输出:

    Array
    (
        [0] => Array
            (
                [0] => 1
                [1] => one
                [2] => uno
            )
    
        [1] => Array
            (
                [0] => 2
                [1] => two
                [2] => dos
            )
    
        [2] => Array
            (
                [0] => 3
                [1] => three
                [2] => tres
            )
    )
    

  • 相关阅读:
    Java类加载器总结
    Java程序编译和运行的过程
    Spring+Struts2+Hibernate整合
    Spring+MyBatis+SpringMVC整合
    声明式事务管理
    Scala sbt 添加国内镜像
    持续天数算法
    idea run shell set user name
    java insert mysql 中文乱码
    Samba服务器 安装
  • 原文地址:https://www.cnblogs.com/jiaoran/p/12636327.html
Copyright © 2011-2022 走看看