php中一个数组到另一个数组的多维数组键值



我有两个数组。

第一个阵列:

Array
(
[0] => Array
    (
        [0] => 17, karol Bagh
        [1] => Noida
        [2] => Delhi
        [3] => India
        [4] => 426001
    )
[1] => Array
    (
        [0] => 117, Srinagar
        [1] => Gaziabad
        [2] => Delhi
        [3] => India
        [4] => 426001
    )
[2] => Array
    (
        [0] => 109,Bangalore
        [1] => Bangalore
        [2] => Karnataka
        [3] => India
        [4] => 560058
    )
[3] => Array
    (
        [0] => Jeevan Shree Building, Ground Floor,S.No.1109, Ganeshkhind Road, Shivajinagar, Near Pune Central
        [1] => Pune
        [2] => Maharashtra
        [3] => India
        [4] => 411005
    )
)

第二个阵列:

Array
(
[0] => Array
    (
        [LatitudeLongitude] => 28.6192015, 77.2791726
    )
[1] => Array
    (
        [LatitudeLongitude] => 28.5615316, 77.268723
    )
[2] => Array
    (
        [LatitudeLongitude] => 13.0525001, 77.4869828
    )
[3] => Array
    (
        [LatitudeLongitude] => 18.5204303, 73.8567437
    )
)

我需要一个数组,比如php中这两个数组的组合:

Array
(
[0] => Array
    (
        [0] => 17, karol Bagh
        [1] => Noida
        [2] => Delhi
        [3] => India
        [4] => 426001
        **[5] => 28.6192015, 77.2791726**
    )
[1] => Array
    (
        [0] => 117, Srinagar
        [1] => Gaziabad
        [2] => Delhi
        [3] => India
        [4] => 426001
        **[5] => 28.5615316, 77.268723**
    )
[2] => Array
    (
        [0] => 109,Bangalore
        [1] => Bangalore
        [2] => Karnataka
        [3] => India
        [4] => 560058
        **[5] => 13.0525001, 77.4869828**
    )
[3] => Array
    (
        [0] => Jeevan Shree Building, Ground Floor,S.No.1109, Ganeshkhind Road, Shivajinagar, Near Pune Central
        [1] => Pune
        [2] => Maharashtra
        [3] => India
        [4] => 411005
        **[5] => 18.5204303, 73.8567437**
    )
)

如何做到这一点?

$index = 0;
foreach ($array2 as $entry) {
    array_push($array1[$index], $entry['LatitudeLongitude']);
    $index++;
}

这个片段循环通过第二个数组,在这个数组中保存lat/lng数据。我们将第二个数组中每个条目的["LatitudeLongitude"]字段,并将其推送到第一个数组中同一索引处的嵌套数组中。递增的变量作为我们在两个数组中的位置的引用。

所以我们总是有$2ndarray[0]['LatitudeLongitude'] -> $1starray[0]$2ndarray[1]['LatitudeLongitude'] -> $1starray[1]。。。。等等(其中$1starray[$i]是阵列本身(。

您可以执行以下操作:

foreach ($array1 as $key => $a) {
    $array1[$key][] = $array2[$key]['LatitudeLongitude'];
}

通过这种方式,您将遍历整个第一个数组,从第二个数组中的右键获取LatittudeLongitude索引。

希望我对有所帮助

相关内容

  • 没有找到相关文章

最新更新