-1

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.

phocks
  • 2,933
  • 5
  • 27
  • 28
  • how did the data arrive in the array initially? – Professor Abronsius Feb 17 '15 at 23:47
  • 1
    any way of doing it will require a loop –  Feb 17 '15 at 23:47
  • possible duplicate of [How to convert a Single Array into a multidimensional array in PHP?](http://stackoverflow.com/questions/19911822/how-to-convert-a-single-array-into-a-multidimensional-array-in-php) – developerwjk Feb 17 '15 at 23:52
  • @RamRaider the data came from a JSON doc sent by another team member as a string that I exploded into an array. I suggested it would be better to send it as proper JSON data, but doesn't look like that's going to happen. – phocks Feb 18 '15 at 00:18
  • @developerwjk this question seems to deal with a different problem, multiple single dimensional arrays into multidimensional, while I am dealing with a single single dimensional array into a single multidimensional... a bit confusing I know. Looks like `Nevershowmyface` has the right answer anyway. – phocks Feb 18 '15 at 00:26
  • @Dagon seems like `array_chunk` was what I was looking for. Thanks – phocks Feb 18 '15 at 00:27

1 Answers1

1

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
)
*/