【问题标题】:Multidimensional Array key value of one array into another array in php一个数组的多维数组键值到php中的另一个数组
【发布时间】:2016-10-18 14:13:46
【问题描述】:

我有两个数组。

第一个数组:

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; for($index=0; $index

标签: php arrays codeigniter multidimensional-array


【解决方案1】:
$index = 0;

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

这个 sn-p 循环遍历第二个数组,您在其中保存 lat/lng 数据。我们正在获取第二个数组中每个条目的 ['LatitudeLongitude'] 字段,并将其推送到存在于第一个数组中相同索引处的嵌套数组中。递增的变量作为我们在两个数组中位置的引用。

所以我们总是有$2ndarray[0]['LatitudeLongitude'] -> $1starray[0]$2ndarray[1]['LatitudeLongitude'] -> $1starray[1]、...等等(其中$1starray[$i] 本身就是一个数组)。

【讨论】:

    【解决方案2】:

    您可以执行以下操作:

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

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

    希望对你有所帮助

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-09
      • 1970-01-01
      • 1970-01-01
      • 2021-06-03
      相关资源
      最近更新 更多