php数组使用多维数组合并键

时间:2022-10-28 16:15:31

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 * and other forums:

与array_merge我设法得到这个(不是我想要的目标),这是我在*和其他论坛上发现的所有内容:

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

使用foreach我可以添加键值,但我正在寻找一种更干净的方式:)

3 个解决方案

#1


1  

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

以下是使用array_map和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)

#2


1  

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.

编辑:您也可以使用array_map()但它不提供任何性能优势AFAIK,并且整体可读性较差。

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

#3


1  

Use array_merge with array_map

将array_merge与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

#1


1  

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

以下是使用array_map和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)

#2


1  

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.

编辑:您也可以使用array_map()但它不提供任何性能优势AFAIK,并且整体可读性较差。

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

#3


1  

Use array_merge with array_map

将array_merge与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