PHP help?

I wrote my first ever JSONP data provider in PHP during my (longish) lunch hour today. For a given “bounded box” of coordinates, it finds the bike paths within that box from OpenStreetMap and returns each path as a set of coordinates so you can use those path points as, for example, a polyline to display an “OSM Bike Path” layer on a Google Map.

I’m an OS kernel engineer, not a PHP web programming guru, so I’m having difficulty solving a problem with arrays and objects. If you know more than a little PHP I’d love a hand.

Update: I’ve figured out typecasting and setting an explicit array index when assigning the latlng pairs works, but I’m still confounded by each “FoundWay” object.

You can view a sample of the JSON string output here. You’ll see the returned data is an array of paths, where each path consists of a “name” and an array of “latlng” objects. Here’s one instance of a path:

"3":{"name":{"0":"Lykins Gulch Trail Crossing"},"latlng":[{"lat":{"0":"40.1641169"},"lng":{"0":"-105.1442890"}},{"lat":{"0":"40.1639857"},"lng":{"0":"-105.1438170"}}]}

In my code, the classes are defined like this.

	class LatLng {
		public $lat;
		public $lng;
	}

	class FoundWay {
		public $name;	         // name of the cycle path
		public $latlng = array(); // array of LatLng objects

To create each “found way” and assign the name to it, I do this:

	$foundway = new FoundWay();
	$foundway->name = FindName($current);

I then create my array of LatLng objects and add each LatLng pair to my path array, like so:

// walk the list of nodes, and lookup lat/lng
// for each node.
foreach($current->nd as $nd) {
  $latlng = new LatLng();
  // find the node lat & lng
  foreach ($parsed_xml->node as $thisnode) {
    if (((integer)$thisnode[id]) == ((integer)$nd[ref])) {
      $latlng->lat = $thisnode[lat];
      $latlng->lng = $thisnode[lon];
      $foundway->latlng[] = $latlng;
    }
  }
}

Finally, each instance of foundway is added to the master array that’s eventually jsonified with json_encode:

	$returnpoints[] = $foundway;

How do I get rid of those “0” things, which I guess are some sort of array index? Do I need to explicity keep an array index for my latlng and returnpoints arrays?

As an aside, it also kind of bugs me that the lat/lon values are strings instead of floats. I’ll try typecasting to see what happens with that.

I appreciate the hand!

2 Comments

  1. Sorry I’m not super familiar with PHP myself, but looking at the JSON you posted it seems the data you’re assigning to $latlng->lat and $latlng->lng are hashes with a key of ‘0’ with the coordinate assigned as the value.

    You could try extracting the value from the hash and assigning it to lat and lng instead.

    // find the node lat & lng
    foreach ($parsed_xml->node as $thisnode) {
    if (((integer)$thisnode[id]) == ((integer)$nd[ref])) {
    $latlng->lat = $thisnode[lat][0];
    $latlng->lng = $thisnode[lon][0];
    $foundway->latlng[] = $latlng;
    }

  2. Richard:

    How are you “still confounded by each “FoundWay” object”?

    You might simplify the code by replacing the LatLng class with a 2-dimensional array since this is straightforward, e.g.:

    $foundway->latlng[] = array($thisnode[‘lat’], $thisnode[‘lon’]);

    Note that associative arrays in PHP are indexed by integers or strings. Some of your values (ref, lat, lon) are actually undefined constant identifiers (PHP is guessing you intended to define them as strings).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.