PHP - 合并两个数组(相同长度)为一个关联? [英] PHP - Merge two arrays (same-length) into one associative?

查看:145
本文介绍了PHP - 合并两个数组(相同长度)为一个关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pretty实际上简单的问题..


  

是否有可能在PHP中同样长度的两个独立的阵列组合一个关联数组,其中第一个数组的值的关联数组作为键?


我可以ofcourse做到这一点,但我正在寻找另一个(内置)功能,或更有效的解决方案..?

 函数组合($数组1,$数组2){
    如果(计数($数组1)==计数($数组2)){
        $ assArray =阵列();
        为($ I = 0; $ I<计数(数组1 $); $ I ++){
            $ assArray [$ ARRAY1 [$ i]] = $数组2 [$ i];
        }
        返回$ assArray;
    }
}


解决方案

array_combine($键,$值)

PS:点击我的答案!它也是一个链接!

pretty straightforward question actually..

is it possible in PHP to combine two separate arrays of the same length to one associative array where the values of the first array are used as keys in the associative array?

I could ofcourse do this, but I'm looking for another (built-in) function, or more efficient solution..?

function Combine($array1, $array2) {
    if(count($array1) == count($array2)) {
        $assArray = array();
        for($i=0;$i<count($array1);$i++) {
            $assArray[$array1[$i]] = $array2[$i];
        }
        return $assArray;
    }
}

解决方案

array_combine($keys, $values)

PS: Click on my answer! Its also a link!

这篇关于PHP - 合并两个数组(相同长度)为一个关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆