0

I know this is a simple concept (albeit one I struggle with) but I have some latitude and longitude data. It is read from a .data file (not as a .db, not my choice) and here is what I have so far:

$current_data = file("/my_data_file.data");
$latest_data = array();
foreach ($current_data as $entry)
{
  $latest_data[] = $entry;
}

$latest_data = preg_replace("!\r?\n!", "", $latest_data);
echo json_encode($latest_data);

This outputs the data like so (didn't paste all of it here to save your eyes from bleeding):

["-118.510 33.896 ","-120.762 32.826 ","-122.959 31.716 ","-125.104 30.570 ","-127.198 29.389 ","-129.243 28.175 ","-131.243 26.931 ","-133.198 25.660 ","-135.112 24.362 ","-136.988 23.041 "]

So it's one giant array, each lat/long pair separated by a comma and each pair within quotations.

I've googled this and there is a ton of information on php arrays and key/values, but what's the right way to do it? I find myself making this much more complicated than it needs to be.

Assuming in the foreach loop it'll be something like

$latest_data[] = array('latitude' => $entry[the_lat_number], 'longitude' => $entry[the_long_number]);

Any input on the matter is appreciated.

Update: example of the data viewed in VIM (It doesn't visually appear there IS a whitespacing issue, but based on using explode, there does seem to be).

enter image description here

3
  • What's your exact question? Why does $latest_data[] = array('latitude' => $entry[the_lat_number], 'longitude' => $entry[the_long_number]); not work? Commented Oct 22, 2013 at 15:27
  • You said you are "assuming it will be something like...", did you try it at all? Commented Oct 22, 2013 at 15:29
  • @Reeno well I'm not going to manually enter the lat or long, that doesn't make any sense. @cillosis Yes of course. It seemed explode was a decent approach but with the inconsistent whitespace I'm not too sure if that is best. Commented Oct 22, 2013 at 15:40

3 Answers 3

1
$current_data = file("/my_data_file.data");
$latest_data = array();
foreach ($current_data as $entry)
{
  list($lat,$lng) = explode(" ",$entry);
  $latest_data[] = array('lat' => $lat, 'lng' => $lng);
}

Now your json data should look something like this:

[{lat: xx, lng: xx},...]
Sign up to request clarification or add additional context in comments.

6 Comments

There must be a spacing issue in the .data file (which I don't have permission to access): because the above returns lat, but no long [{"lat":"-118.510","lng":""},{"lat":"-120.762","lng":""},{"lat":"-122.959","lng":""}]
Look in the file and see how it is delimited. I just assumed that it would be space, given the resulting json you posted. Maybe you can expand your question with a sample of the data file ( like one or two lines )
Updated original question with attached image.
It looks like the data is delimeted by multiple spaces or maybe space / tabs. Do this inside your foreach loop: print_r(explode(" ",$entry)); and you'll be able to see exactly how the lines are delimited. Then adjust your explode function accordingly. It looks like lat and long are separated by four spaces
Yeah I just messed with this: codeshare.io/d4FPp but I'm thinking I have to use preg_split instead of explode. Let me know your thoughts (you can edit that codeshare/write comments and I'll be able to see them).
|
0

Using your code to add each pair as subarray is good approach. This way you'll be able to easily access data from each row like that $latest_data[3]['latitude'] (to get latitude from fourth row).

Comments

0

Ok I ended up using preg_split instead of explode and all seems ok:

$current_data = file("/tmp/navhome/data/path_tail.data");

$latest_data = array();
foreach ($current_data as $entry) {
  $coords = preg_split("/ +/", $entry);
  $latest_data[] = array('latitude' => $coords[0], 'longitude' => $coords[1]);

}

echo json_encode($latest_data);

This is airplane data that gets updated when the flight path changes so it seemed preg_split was more appropriate.

This is outputting the data like so:

[{"latitude":"-118.510","longitude":"33.896"},{"latitude":"-120.762","longitude":"32.826"},{"latitude":"-122.959","longitude":"31.716"}]

Appreciate the input from everyone. Criticisms are welcome on this solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.