How to concatenate two or more arrays in PHP without loosing values if it is same key and different values

瘦欲@ 提交于 2020-01-05 03:08:50

问题


I have two multi-dimensional arrays. I need to concatenate the two without loosing any values which have the same key and different values. Here is the scenario:

Array1
(
    [0] => 11
    [2] => 12
    [3] => 13
    [4] => (
                [0] =>  100
                [1] =>  200
            )
    [5] => 2
    [6] => 3
)

Array2
(
    [0] => 11
    [2] => 12
    [3] => 13
    [4] => (
                [0] =>  400
                [1] =>  500
            )
    [5] => 2
    [6] => 3
)

The result should be

Result
(
    [0] => 11
    [2] => 12
    [3] => 13
    [4] => (
                [0] =>  (
                            [0] => 100
                            [1] => 400
                        )
                [1] =>  (
                            [0] => 200
                            [1] => 500
                        )
            )
    [5] => 2
    [6] => 3
)

回答1:


Here is one solution:

<?php
$arrayA = array(0 => 11, 2 => 12, 3 => 13, 4 => array(0 => 100, 1 => array(0 => 222),), 5 => 2, 6 => 3);
$arrayB = array(
    0 => 11,
    2 => 12,
    3 => 13,
    4 => array(
        0 => 100,
        1 => array(0 => array(0 => 'test1', 1 => 'test2'), 1 => array(0 => 'test1', 1 => 'test2'),),
    ),
    5 => 2,
    6 => 3
);


/**
 * @param $a
 * @param $b
 * @return array
 */
function array_merge_graceful($a, $b)
{
    $c = [];
    if (is_array($a) && is_array($b)) {
        foreach (array_merge(array_keys($a),array_keys($b)) as $i) {
            if (!array_key_exists($i, $a)) {
                $c[$i] = $b[$i];
            } elseif (!array_key_exists($i, $b)) {
                $c[$i] = $a[$i];
            } else {
                $c[$i] = array_merge_graceful($a[$i], $b[$i]);
            }
        }
    } else {
        if ($a <> $b) {
            $c = [$a, $b];
        } else {
            $c = $a;
        }
    }
    return $c;
}

var_dump(array_merge_graceful($arrayA, $arrayB));

?>



回答2:


Try something like this

<?php

$array1 = array(11, 12, 13, array(100, 200), 2, 3);
$array2 = array(11, 12, 13, array(400, 500), 2, 3);
echo "<pre>";
print_r($array1);
print_r($array2);

$combine = array();

foreach ($array1 as $key => $value) {
    if (array_key_exists($key, $combine)) {
        //if is array
        if (is_array($combine[$key])) {
            $combine[$key] = array($combine[$key], $value);
        }
    } else {
        $combine[$key] = $value;
    }
}

foreach ($array2 as $key => $value) {
    if (array_key_exists($key, $combine)) {
        //if is array
        if (is_array($combine[$key])) {
            $combine[$key] = array($combine[$key], $value);
        }
    } else {
        $combine[$key] = $value;
    }
}

echo "<hr>";
print_r($combine);



回答3:


use array_merge_recursive() function for merging arrays.For more details please refer http://php.net/manual/en/function.array-merge-recursive.php



来源:https://stackoverflow.com/questions/30937328/how-to-concatenate-two-or-more-arrays-in-php-without-loosing-values-if-it-is-sam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!