0
Array
(
    [0] => Array
        (
            [id] => 123-456-000-000
            [name] => john
        )

    [1] => Array
        (
            [id] => 123-456-789-014
            [name] => james
        )

)
Array
(
    [0] => Array
        (
            [id] => 123-456-000-000
            [address] => Japan
        )

    [1] => Array
        (
            [tin] => 123-456-789-014
            [address] => Spain
        )

)

I have 2 array above and I want to combine them so i used array-merge. But I didnt get the result i want. What it gave me is

Array
(
    [0] => Array
        (
            [id] => 123-456-000-000
            [name] => john
        )

    [1] => Array
        (
            [id] => 123-456-789-014
            [name] => james
        )

    [2] => Array
        (
            [tin] => 123-456-000-000
            [address] => Japan
        )

    [3] => Array
        (
            [tin] => 123-456-789-014
            [address] => Spain
        )

)

What i wanted is

Array
(
    [0] => Array
        (
            [id] => 123-456-000-000
            [name] => john
            [address] => Japan
        )

    [1] => Array
        (
            [id] => 123-456-789-014
            [name] => james
            [address] => Spain
        )

)

How to achieve this kind of array merge

guradio
  • 15,524
  • 4
  • 36
  • 57
  • Will you always have arrays of equal length? –  Dec 09 '15 at 04:14
  • @Terminus what do you mean by equal length?if you mean same number of array yes always.but there are more values i just shorten them. in first array there are last name etc. while second one it breaks down to country city district..i hope i answered it correctly – guradio Dec 09 '15 at 04:17
  • @Terminus because they are pair...in one array it is personal information of the person and the other is the address information – guradio Dec 09 '15 at 04:19
  • Just compare one by one. Take a look here http://stackoverflow.com/questions/13669554/merging-two-multidimensional-arrays-on-specific-key It might solve your problem but I am sure it isn't the best way. – Thuong Nguyen Dec 09 '15 at 04:32

2 Answers2

2

Assume both array have the same number of elements and in the same order, then one foreach loop can do the job

$arr1 = Array (
    0 => Array (
        "id" => "123-456-000-000",
        "name" => "john"
    ),
    1 => Array (
        "id" => "123-456-789-014",
        "name" => "james"
    )

);
$arr2 = Array (
    0 => Array (
        "tin" => "123-456-000-000",
        "address" => "Japan"
    ),
    1 => Array (
        "tin" => "123-456-789-014",
        "address" => "Spain"
    )
);

foreach ($arr2 as $key => $inner) {
    $arr1[$key]["address"] = $inner["address"];
}

output: $arr1

Array
(
    [0] => Array
        (
            [id] => 123-456-000-000
            [name] => john
            [address] => Japan
        )

    [1] => Array
        (
            [id] => 123-456-789-014
            [name] => james
            [address] => Spain
        )

)
Andrew
  • 2,810
  • 4
  • 18
  • 32
  • 1
    It would have been much better if you made the fiddle for the same.. I've done that for you.. Check it: https://3v4l.org/msWNs – Saiyan Prince Dec 09 '15 at 04:34
  • 1
    thanks, I usually just post it here and have never thought of posting on external site – Andrew Dec 09 '15 at 04:38
  • @user3514160 @ Andrew Great help guys appreacited – guradio Dec 09 '15 at 05:03
  • 1
    @MLM Glad to hear that.. @ Andrew Most welcome. Try to include the fiddle next time as it will help the future visitors who stumble upon to this question finding for the solution. – Saiyan Prince Dec 09 '15 at 05:09
0

Supposing that your arrays are at the same size and that your IDs are ordered int he same way.

Then you can try:

$arrayReturn = array();
foreach( $array1 as $key1 => $result1 ){
    $mergeArray = [$result1['id'], $result1['name'], $array2[$key1]['name']];
    array_push($arrayReturn, $mergeArray);
}

Where $array1 and $array2 are both of my initial arrays. $arrayReturn is my result. Is there no other ways for you to directly merge the arrays at their creation? It would be much more elegant and will avoid all that.

maxime_039
  • 464
  • 8
  • 14