duanjian4698 2015-02-17 23:41
浏览 49
已采纳

在PHP中将重复的单维数组转换为多维数组

I have a single dimentional PHP Array that has latitude, longitude, and time data. The data goes eg [lat, long, date, lat, long, date, lat, long, date.... etc etc]

Array ( [0] => -28.0447606 [1] => 153.4340961 [2] => 1424136836118 [3] => -28.0447612 [4] => 153.4340963 [5] => 1424136876189 [6] => -28.0447658 [7] => 153.4340962 [8] => 1424136897993 [9] => -28.0447619 [10] => 153.4340615 [11] => 1424136918045 [12] => -28.0447656 [13] => 153.434057 [14] => 1424136938057 [15] => -28.0447613 [16] => 153.4340484 [17] => 1424136958085 [18] => -28.0447791 [19] => 153.4340959 [20] => 1424136978117 [21] => -28.0447584 [22] => 153.4340501 [23] => 1424136998135 [24] => -28.0447676 [25] => 153.434047 [26] => 1424137018179 [27] => -28.044782 [28] => 153.4340982 [29] => 1424137038185 [30] => -28.0447599 [31] => 153.4340496 [32] => 1424137058214 [33] => -28.0447614 [34] => 153.4340531 [35] => 1424137078589 [36] => -28.0447588 [37] => 153.4340963 [38] => 1424137098731 [39] => -28.0447768 [40] => 153.434098 [41] => 1424137138640 [42] => -28.0447141 [43] => 153.4341097 [44] => 1424137158672 [45] => -28.0447628 [46] => 153.4340962 [47] => 1424137178698 [48] => -28.0447622 [49] => 153.4340962 [50] => 1424137198726 [51] => -28.0447528 [52] => 153.4340936 [53] => 1424137218871 [54] => -28.0447636 [55] => 153.4340472 [56] => 1424137258825 [57] => -28.0447608 [58] => 153.434097 [59] => 1424137279945 [60] => -28.0447656 [61] => 153.4340979 [62] => 1424137300018 )

I am just wondering if there is an easy way to convert this into a two dimensional array so I can access them like $gps[0][1] or $gps[3][0] etc. I've tried a few ways like using a for loop, but surely there's some other way I'm overlooking.

  • 写回答

1条回答 默认 最新

  • douxingti9307 2015-02-18 00:00
    关注

    If I'm not wrong this is what you want. You just need to change the $arr for your variable.

    $coords = array_map(function($coords) {
      return array(
        "lat" => $coords[0],  # latitude
        "long" => $coords[1], # longitude
        "ts" => $coords[2]    # timestamp
      );
    }, array_chunk($arr, 3));
    
    /*
    Array (
      Array (
        [lat] => 12
        [long] => 34
        [ts] => 56
      )
      Array (
        [lat] => 12
        [long] => 34
        [ts] => 56
      )
      ... and so on
    )
    */
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?