如何将第一个数组元素添加到第二个数组

时间:2020-05-14 08:40:39

标签: php

这是第一个数组

Array
(
    [0] => Array
        (
            [store_lat] => 25.2029
            [store_lng] => 51.4349
        )

    [1] => Array
        (
            [store_lat] => 25.2029
            [store_lng] => 51.4349
        )

)

这是第二个数组

Array
(
    [0] => Array
        (
            [cust_lat] => 25.2029
            [cust_lng] => 51.4349
        )

    [1] => Array
        (
            [cust_lat] => 25.2029
            [cust_lng] => 51.4349
        )

)

我想要这种格式

Array
(
    [0] => Array
        (
            [cust_lat] => 25.2029
            [cust_lng] => 51.4349
            [store_lat] => 25.2029
            [store_lng] => 51.4349
        )

    [1] => Array
        (
            [cust_lat] => 25.2029
            [cust_lng] => 51.4349
            [store_lat] => 25.2029
            [store_lng] => 51.4349
        )

)

2 个答案:

答案 0 :(得分:0)

您可以尝试this吗,这是在PHP中合并数组的示例

<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>

答案 1 :(得分:0)

假设两个数组的长度始终相等(或者第二个数组的长度始终大于第一个数组的长度),则可以循环第一个数组并使用键从辅助数组访问值。

$result = [];
foreach($stores as $key=>$store) {
    $result[] = array_merge($store, $custs[$key]);
}
print_r($result);