阵列多维数组合并 [英] array merge with multi dimentional array

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

问题描述

我有2个阵列。我想将它们合并。

I have 2 arrays. I want to merge them.

阵列1:

Array
(
[100] => 
[50] => 
[CREDIT] => 14.31
[CHEQUE] => 
)

阵列2:

Array
(
[0] => Array
    (
        [id] => 501
    )
[1] => Array
    (
        [id] => 502
    )
[2] => Array
    (
        [id] => 503
    )

我与 $数组1 + $数组2 将它们合并后得到这样的结果:

I am getting this result after merging them with $array1+$array2:

Array
(
[100] => 
[50] => 
[CREDIT] => 14.31
[CHEQUE] => 
[0] => Array
    (
        [id] => 501
    )
)

和我预期的结果是:

Array
(
[100] => 
[50] => 
[CREDIT] => 14.31
[CHEQUE] => 
[0] => Array
    (
    [0] => Array
        (
            [id] => 501
        )
    [1] => Array
        (
            [id] => 502
        )
    [2] => Array
        (
            [id] => 503
        )
    )
)

这可能是什么原因?谢谢你。

What could be the reason? Thanks.

推荐答案

这将与你和所需要的许多方面需要尽可能多的数组。

These will work with as many arrays as you need and with as many dimensions needed.

有关这些功能,设定最高优先级排列到最低指数:

For these functions, set the highest priority array to the lowest index:

function overlapArrays($array) {
    if(count($array) > 1) {
        for($i = (count($array) - 1);$i >= 0;$i--) {
            if(($i - 1) >= 0) {
                prioritize($array[$i], $array[$i - 1]);
            }
            else {
                break;
            }
        }
        return $array[0];
    }
    else {
        return $array;
    }
}

function prioritize($arr1, &$arr2) {
    foreach($arr1 as $key => $value) {
        if(!array_key_exists($key, $arr2)) {
            $arr2[$key] = $value;
        }
        elseif(is_array($value) && is_array($arr2[$key])) {
            prioritize($value, $arr2[$key]);
        }
    }
}

$array1 = [
    100 => '',
    50 => '',
    'CREDIT' => 14.31,
    'CHEQUE' => ''
];

$array2 = [
    0 => [
        'id' => 501
    ],
    1 => [
        'id' => 502
    ],
    2 => [
        'id' => 503
    ]
];

$merger = Array($array1, $array2);

$results = overlapArrays($merger);

例如,如果希望同时存在的第一阵列和第二阵列中是第一个阵列中的preserved键,那么你将第一个数组合并数组中的索引。如果您想在有比赛ARRAY2至preserve值,然后分配给数组2 0指数。

For instance, if you want keys that exist both in the first array and second array to be preserved in the first array then you assign the first array to the 0 index in the merge array. If you want array2 to preserve values when there are matches, then assign array2 to the 0 index.

$array1 = Array(
    'one' => Array(
         'two' => 2,
         'three' => 3
    )
);
$array2 = Array(
    'one' => Array(
        'two' => 3,
    )
);
$merger = Array($array2, $array1);

$results = overlapArrays($merger);

结果=

Array(
    'one' => Array(
        'two' => 3,
        'three' => 3
    )
);

与合并数组中的最高索引的阵列具有最低优先级。与合并数组中的最低索引的阵列具有最高优先级。让我知道,如果你有足够的问题。我想确保我解释的不够好。

The array with the highest index in the merger array has the lowest priority. The array with the lowest index in the merger array has the highest priority. Let me know if you have enough questions. I'm trying to make sure I explained it well enough.

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

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