2

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
)
s1lam8u
  • 111
  • 1
  • 3
  • 13
  • 3
    So... what have you tried until now? – Federkun Jun 19 '15 at 11:48
  • possible duplicate of [PHP: merge two arrays while keeping keys instead of reindexing?](http://stackoverflow.com/questions/3292044/php-merge-two-arrays-while-keeping-keys-instead-of-reindexing) – vitr Jun 19 '15 at 11:55
  • it's just + operator, and also a duplicate question)) – vitr Jun 19 '15 at 11:56
  • both comments not correct! – codeneuss Jun 19 '15 at 12:00
  • i have tried If I have key & value in both arrays, It should take only one index, Below my code. $ar1 = array("color" => array("favorite" => "red"), 5); $ar2 = array(10, "color" => array("favorite" => "red", "blue")); $result = array_merge_recursive($ar1, $ar2); echo "
    "; print_r($result); echo "
    "
    – s1lam8u Jun 19 '15 at 13:08

3 Answers3

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));

?>
codeneuss
  • 905
  • 4
  • 12
  • This is woking... But for this scenario is not working please help me out. $array1 = array( 0 => 11, 2 => 12, 3 => 13, 4 => array( 0 => 100, 1 => array( 0 => 222 ), ), 5 => 2, 6 => 3 ); $array2 = 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 ); – s1lam8u Jun 19 '15 at 12:53
  • what do you expect as result ? – codeneuss Jun 19 '15 at 13:07
  • I have an API response which gives a bunch of objects from that, multiple objects which has the same key and different values sometime same key and value. So I need to concatenate But the problem is I don't know whether I will get 2 dimensional or 3 dimensional. Sometime I have to merge 3 arrays. – s1lam8u Jun 19 '15 at 13:19
  • okay .. i got it now .. try out and tell me if it works for you. – codeneuss Jun 19 '15 at 14:56
-1

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);
Faiz Rasool
  • 1,379
  • 1
  • 12
  • 20
-1

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

sonam gupta
  • 775
  • 6
  • 17
  • If I have key & value in both arrays, It should take only one index, Below my code. $ar1 = array("color" => array("favorite" => "red"), 5); $ar2 = array(10, "color" => array("favorite" => "red", "blue")); $result = array_merge_recursive($ar1, $ar2); echo "
    ";
       print_r($result);
       echo "
    ";
    – s1lam8u Jun 19 '15 at 12:38
  • it may work with string keys .. but not with integer keys... i've tried it with the arrays from the question... its not always rtfm – codeneuss Jun 19 '15 at 13:09