如何将两个数组组合在一起? [英] How to combine two arrays together?

查看:46
本文介绍了如何将两个数组组合在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种快速的方法可以将一个数组的值组合为另一个数组的键?

输入:

array A =>大批 ([0] =>猫"[1] =>蝙蝠"[2] =>帽子"[3] =>垫")数组B =>大批 ([0] =>毛皮"[1] =>球"[2] =>衣服"[3] =>家")

预期输出:

数组 C =>大批 ([猫] =>毛皮"[蝙蝠] =>球"[帽子] =>衣服"[垫子] =>家")

我怎么能这样做?

解决方案

array_combine() 完全可以满足您的需求.

引用手册:

<块引用>

array array_combine ( array $keys , array $values )

使用keys数组中的值作为key,values数组中的值作为对应的值来创建一个数组.

就您而言,您必须执行以下操作:

$array['C'] = array_combine($array['A'], $array['B']);


当然,您也可以使用各种循环组合来做到这一点,array_combine() 可能是最简单的解决方案.

Is there a quick way to combine one arrays values as the other array's keys?

Input:

array A => Array (
        [0] => "cat"
        [1] => "bat"
        [2] => "hat"
        [3] => "mat"
    )

array B => Array (
        [0] => "fur"
        [1] => "ball"
        [2] => "clothes"
        [3] => "home"
    )

Expected output:

array C => Array (
        [cat] => "fur"
        [bat] => "ball"
        [hat] => "clothes"
        [mat] => "home"
    )

How could I do that?

解决方案

array_combine() will exactly do what you want.

Quoting the manual:

array array_combine ( array $keys , array $values )

Creates an array by using the values from the keys array as keys and the values from the values array as the corresponding values.

In your case, you'd have to do something like this:

$array['C'] = array_combine($array['A'], $array['B']);


While of course you could also use various combinations of loops to do that, array_combine() is probably the simplest solution.

这篇关于如何将两个数组组合在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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