合并多维数组 PHP 而不改变键

Merge multidimensional Array PHP without changing the key

我在合并这个数组时遇到问题,我想在下面合并这个数组

    Array
    (
       [0] => Array
           (
               [image] => logo.jpg
               [name] => data
               [server] => Mirani Rahmawati
               [6] => 5
           )

       [1] => Array
           (
               [image] => logo.jpg
               [name] => data
               [server] => Mirani Rahmawati
               [5] => 150
           )
       )
   

预期的结果是这样的

Array
(
    [0] => Array
        (
            [image] => logo.jpg
            [name] => data
            [server] => Mirani Rahmawati
            [6] => 5
            [5] => 150
        )

)

不更改密钥..

谢谢。

您可以使用 Array Operators + 来做到这一点。你可以查看我下面的演示:

<?php

$array = [
  array(
    "image" => "logo.jpg",
    "name" => "data",
    "server"=> "Mirani Rahmawati",
     "6" => "5"
  ),
  array(
    "image"=> "logo.jpg",
    "name" => "data",
    "server"=> "Mirani Rahmawati",
     "5" => "150"
  )
];

$result = [];
foreach ($array as $item) {
   $result = $result + $item;
}

print_r($result);
exit;

?>

结果将是:

Array
(
    [image] => logo.jpg
    [name] => data
    [server] => Mirani Rahmawati
    [6] => 5
    [5] => 150
)

您可以在 https://www.php.net/manual/en/language.operators.array.php

找到更多信息
<?php
$array = array();
$array[]  = array(
    "image" => "logo.jpg",
    "name" => "data",
    "server" => "Mirani Rahmawati",
    6 => 5
);
$array[]  = array(
    "image" => "logo.jpg",
    "name" => "data",
    "server" => "Mirani Rahmawati",
    5 => 150    
);

$new_array = array($array[0] + $array[1]);
var_dump($new_array);

Array Merge - Contribute Notes