• php函数 array_combine


     

    (PHP 5, PHP 7)

    array_combine — 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值

     

    array_combine ( array $keys , array $values ) : array

    返回一个 array,用来自 keys 数组的值作为键名,来自 values 数组的值作为相应的值。

     

    keys

    将被作为新数组的键。非法的值将会被转换为字符串类型(string)。

    values

    将被作为 Array 的值。

     

    返回合并的 array,如果两个数组的单元数不同则返回 FALSE

    <?php
    /**
     * Created by PhpStorm.
     * User: mac
     * Date: 2019/4/13
     * Time: 10:10
     */
    
    $arr1 = ['a','b','c'];
    $arr2 = [1,2,3,4];
    
    $res = array_combine($arr1,$arr2);
    echo "<pre>";
    print_r($res); //
    

     上面返回 Warning: array_combine(): Both parameters should have an equal number of elements in /www/wang/functions/combine.php on line 12

    参数不一致

     

    $arr1 = ['a','b','c'];
    $arr2 = [1,2,3];
    
    $res = array_combine($arr1,$arr2);
    echo "<pre>";
    print_r($res);
    

     上面返回

    Array
    (
        [a] => 1
        [b] => 2
        [c] => 3
    )

    $arr1 = ['a','b','c'];
    $arr2 = [1,2,3];
    
    $res = array_combine($arr2,$arr1);
    echo "<pre>";
    print_r($res);
    

     上面返回

    Array
    (
        [1] => a
        [2] => b
        [3] => c
    )

     

  • 相关阅读:
    python base64加密文本内容(1)
    python 翻译爬虫
    json为txt文本加密
    python json
    repr()函数
    linux 基本命令
    测试管理工具--禅道
    测试基础理论
    测试用例--场景法
    测试用例--测试大纲(提纲)法
  • 原文地址:https://www.cnblogs.com/brady-wang/p/10700037.html
Copyright © 2020-2023  润新知