合并两个多维数组并重新索引所有子数组 [英] Merge two multidimensional arrays and reindex all subarrays

查看:28
本文介绍了合并两个多维数组并重新索引所有子数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

第一个数组:

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

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

第二个数组:

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.

推荐答案

FIXED(再次)

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:提高可读性和效率

EDIT 03/2014: Improved readability and efficiency

这篇关于合并两个多维数组并重新索引所有子数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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