简体   繁体   中英

php array merge on key using multidimensional array

This is my array:

array 
  0 => 
    array 
      'id' => int 220950
      'order_reference' => string '600125479' 
  1 => 
    array 
      'id' => int 220985
      'order_reference' => string '498638' 

and this my another array

array 
      0 => 
        array 
          'entity_id' => 1
          'order_status' => 'test' 
      1 => 
        array 
          'entity_id' => 2
          'order_status' => 'test2' 

and my goal is to achieve this:

array 
      0 => 
        array 
          'id' => int 220950
          'order_reference' => string '600125479'
          'entity_id' => 1
          'order_status' => 'test' 
      1 => 
        array 
          'id' => int 220985
          'order_reference' => string '498638'
          'entity_id' => 2
          'order_status' => 'test2'

with array_merge I managed to get this(NOT my desired goal) and this all I found on stackoverflow and other forums:

array 
      0 => 
        array 
          'id' => int 220950
          'order_reference' => string '600125479' 
      1 => 
        array 
          'id' => int 220985
          'order_reference' => string '498638' 
         array 
      2 => 
        array 
          'entity_id' => 1
          'order_status' => 'test'
      3 => 
        array 
          'entity_id' => 2
          'order_status' => 'test2'

Any ideas or suggestions are welcomed :) thank you

Using a foreach I can add the keys values, but I am looking for a more cleanest way :)

With the caveat that your arrays don't have a joint value to compare against so you need to make sure that the keys always line up, I think a loop like this will do the trick:

for ($i = 0; $i < count($array1); $i++)
{
    $new_array[] = array_merge($array1[$i], $array2[$i]);
}

Edit: You can also use array_map() but it doesn't offer any performance advantages AFAIK, and is overall less readable.

$new_array = array_map(function($a1_v, $a2_v) { return array_merge($a1_v, $a2_v); }, $a1, $a2);

Here's the answer to your question using array_map and array_merge_recursive .

<?php

$array1 = array(
    [
        "id" => 220950,
        "order_reference" => "600125479"
    ],
    [
        "id" => 220985,
        "order_reference" => "498638"
    ]
);

$array2 = array(
    [
        "entity_id" => 1,
        "order_status" => "test"
    ],
    [
        "entity_id" => 2,
        "order_status" => "test"
    ]
);

$results = array();

array_map(function($array1, $array2) use (&$results) {

    $results[] = array_merge_recursive($array1, $array2);

}, $array1, $array2);

var_dump($results);

This will output:

array (size=2)
  0 => 
    array (size=4)
      'id' => int 220950
      'order_reference' => string '600125479' (length=9)
      'entity_id' => int 1
      'order_status' => string 'test' (length=4)
  1 => 
    array (size=4)
      'id' => int 220985
      'order_reference' => string '498638' (length=6)
      'entity_id' => int 2
      'order_status' => string 'test' (length=4)

Use array_merge with array_map

    $array1 = array(
    [
        "id" => 220950,
        "order_reference" => "600125479"
    ],
    [
        "id" => 220985,
        "order_reference" => "498638"
    ]
);

$array2 = array(
    [
        "entity_id" => 1,
        "order_status" => "test"
    ],
    [
        "entity_id" => 2,
        "order_status" => "test2"
    ]
);

$result = array_map("array_merge",$array1,$array2);
print_r($result);

Output

    Array
(
    [0] => Array
        (
            [id] => 220950
            [order_reference] => 600125479
            [entity_id] => 1
            [order_status] => test
        )

    [1] => Array
        (
            [id] => 220985
            [order_reference] => 498638
            [entity_id] => 2
            [order_status] => test2
        )

)

Working example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM