php php合并两个数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8561987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:59:48  来源:igfitidea点击:

php merge two arrays

phparraysmerge

提问by Rajesh Shrestha

I have two arrays, I want to merge these two arrays into single array. Please view the detail below:

我有两个数组,我想将这两个数组合并为一个数组。请查看以下详细信息:

First Array:

第一个数组:

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

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

Second Array:

第二个数组:

Array
(
    [0] => Array
        (
            [d] => 4
            [e] => 5
            [f] => 6
        )

    [1] => Array
        (
            [d] => 6
            [e] => 5
            [f] => 4
        )
)

I want this result. Does somebody know how to do this?

我想要这个结果。有人知道怎么做吗?

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [1] => Array
        (
            [0] => 3
            [1] => 2
            [2] => 1
        )
    [2] => Array
        (
            [0] => 4
            [1] => 5
            [2] => 6
        )

    [3] => Array
        (
            [0] => 6
            [1] => 5
            [2] => 4
        )
)

Hope you have understand the question. Thank you in advance.

希望你已经理解了这个问题。先感谢您。

采纳答案by DaveRandom

FIXED(again)

固定(再次)

function array_merge_to_indexed () {
    $result = array();

    foreach (func_get_args() as $arg) {
        foreach ($arg as $innerArr) {
            $result[] = array_values($innerArr);
        }
    }

    return $result;
}

Accepts an unlimited number of input arrays, merges all sub arrays into one container as indexed arrays, and returns the result.

接受无限数量的输入数组,将所有子数组合并到一个容器中作为索引数组,并返回结果。

EDIT 03/2014:Improved readability and efficiency

编辑 03/2014:提高可读性和效率

回答by Nakkeeran

Try array_merge:

尝试array_merge

$result = array_merge($array1, $array2);

回答by pleerock

more simple and modern way is:

更简单和现代的方法是:

$merged = $array1 + ['apple' => 10, 'orange' => 20] + ['cherry' => 12, 'grape' => 32];

new array syntax from php 5.4

来自php 5.4 的新数组语法

回答by liquorvicar

If you want to return the exact result you specify in your question then something like this will work

如果您想返回您在问题中指定的确切结果,那么这样的事情将起作用

function array_merge_no_keys() {
    $result = array();
    $arrays = func_get_args();
    foreach( $arrays as $array ) {
        if( is_array( $array ) ) {
            foreach( $array as $subArray ) {
                $result[] = array_values( $subArray );
            }
        }
    }
    return $result;
}

回答by PiTheNumber

array_mergedoes the trick:

array_merge可以解决问题:

$a = array(array(1,2,3), array(3,2,1));
$b = array(array(4,5,6), array(6,5,4));
var_dump(array_merge($a, $b));

Try it: http://codepad.org/Bf5VpZOr

试试看:http: //codepad.org/Bf5VpZOr

Exactly the wanted result.

正是想要的结果。

回答by Roland

You could also use : array_merge_recursive; for multidimensional arrays.

你也可以使用:array_merge_recursive; 对于多维数组。

<?php
    $ar1 = array("color" => array("favorite" => "red"), 5);
    $ar2 = array(10, "color" => array("favorite" => "green", "blue"));
    $result = array_merge_recursive($ar1, $ar2);
    print_r($result);
?>

Result :

结果 :

Array(
    [color] => Array
        (
            [favorite] => Array
                (
                    [0] => red
                    [1] => green
                )

            [0] => blue
        )

    [0] => 5
    [1] => 10
)

Source : PHP Manual.

来源:PHP 手册