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!

Comments are closed.

Exchange a link
Click HERE to exchange a link
Polls

Contador guilty of using doping?

View Results

Loading ... Loading ...