内核键上的PHP数组合并

时间:2021-12-15 10:45:22

I have two arrays like:

我有两个数组,如:

$a = [
    0 => [
        'price' => 5.5
    ],
    1 => [
        'price' => 6.0
    ],
    2 => [
        'price' => 6.2
    ],
    3 => [
        'price' => 6.5
    ],
];

$b = [
    0 => [
        'color' => 'RED'
    ],
    1 => [
        'color' => 'WHITE'
    ],
    2 => [
        'color' => 'BLUE'
    ],
    3 => [
        'color' => 'RED'
    ],
];

I should have this response:

我应该有这样的回应:

Array
(
    [0] => Array
        (
            [price] => 5.5
            [color] => RED
        )
    [1] => Array
        (
            [price] => 6
            [color] => WHITE
        )
    [2] => Array
        (
            [price] => 6.2
            [color] => BLUE
        )
    [3] => Array
        (
            [price] => 6.5
            [color] => RED
        )
)

I heard about the function: array_merge_recursive but the response wasn't the requiered:

我听说过函数:array_merge_recursive但是响应不是requiered:

Array
(
    [0] => Array
        (
            [price] => 5.5
        )
    [1] => Array
        (
            [price] => 6
        )
    [2] => Array
        (
            [price] => 6.2
        )
    [3] => Array
        (
            [price] => 6.5
        )
    [4] => Array
        (
            [color] => RED
        )
    [5] => Array
        (
            [color] => WHITE
        )

    [6] => Array
        (
            [color] => BLUE
        )
    [7] => Array
        (
            [color] => RED
        )
    )

so I decided to write my own function:

所以我决定编写自己的函数:

function merge ($a, $b) {
    $keys = array_keys($a);
    foreach ($keys as $value) {
        if (isset($b[$value])) {
            $tmp = array_keys($b[$value]);
            foreach ($tmp as $val){
                $a[$value][$val] = $b[$value][$val];
            }
        }
    }
    return $a;
}
print_r(merge($a, $b));

and I got the proper response:

我得到了适当的回应:

Array
(
    [0] => Array
        (
            [price] => 5.5
            [color] => RED
        )
    [1] => Array
        (
            [price] => 6
            [color] => WHITE
        )
    [2] => Array
        (
            [price] => 6.2
            [color] => BLUE
        )
    [3] => Array
        (
            [price] => 6.5
            [color] => RED
        )
)

The problem is that it works fine for little arrays but doesn't work good for big arrays, so my question is: how could I optimize the function? because the complexity would grow depending on merged keys.

问题是它适用于小数组,但对大数组不起作用,所以我的问题是:我怎样才能优化函数?因为复杂性会增加,具体取决于合并的密钥。

Using PHP 7.0

使用PHP 7.0

3 个解决方案

#1


4  

You can use array_replace_recursive() instead.

您可以使用array_replace_recursive()代替。

array_replace_recursive($a, $b);

Demo: https://3v4l.org/bFIZ2

演示:https://3v4l.org/bFIZ2

#2


4  

You need to check that they are the same length and then one simple foreach is all that is needed:

您需要检查它们的长度是否相同,然后只需要一个简单的foreach:

foreach($a as $k => $v) {
    $result[$k] = array_merge($v, $b[$k]);
}

#3


0  

the simple solution might be,

简单的解决方案可能是,

$c = [];

for($i=0; $i<count(array_keys($a)); $i++) {
  $c[$i] =array_merge($a[$i], $b[$i]);
}

print_r($c);

https://repl.it/KJNL

https://repl.it/KJNL

#1


4  

You can use array_replace_recursive() instead.

您可以使用array_replace_recursive()代替。

array_replace_recursive($a, $b);

Demo: https://3v4l.org/bFIZ2

演示:https://3v4l.org/bFIZ2

#2


4  

You need to check that they are the same length and then one simple foreach is all that is needed:

您需要检查它们的长度是否相同,然后只需要一个简单的foreach:

foreach($a as $k => $v) {
    $result[$k] = array_merge($v, $b[$k]);
}

#3


0  

the simple solution might be,

简单的解决方案可能是,

$c = [];

for($i=0; $i<count(array_keys($a)); $i++) {
  $c[$i] =array_merge($a[$i], $b[$i]);
}

print_r($c);

https://repl.it/KJNL

https://repl.it/KJNL