I need to pass a PHP array to a javascript function where the array has the form
var Waypoints = [{"Geometry":{"Latitude":52.34,"Longitude":55.35}},{"Geometry":{"Latitude":55.34,"Longitude":56.35}}]
Here is a code snippet:
class Geometry
{
public $Latitude;
public $Longitude;
}
class WayPoint
{
public $Geometry;
}
$Geometry = new Geometry();
$wp = new WayPoint();
$wp->Geometry->Latitude = 52.34;
$wp->Geometry->Longitude = 55.35;
$wp2 = new WayPoint();
$wp2->Geometry->Latitude = 55.34;
$wp2->Geometry->Longitude = 56.35;
$php_data = [$wp,$wp2 ];
echo json_encode($php_data);
which produces this:
[{"Geometry":{"Latitude":52.34,"Longitude":55.35}},{"Geometry":{"Latitude":55.34,"Longitude":56.35}}]
This is the correct array structure, however I am asking two questions:
- Is this the best way of declaring the object classes with regarding to filling the array with a very large number of co-ordinates, and
- Why is PHP giving a warning for the code on lines, 13 and 16 regarding empty values, i.e.
Warning: Creating default object from empty value in for $wp->Geometry->Latitude = 52.34; and $wp2->Geometry->Latitude = 55.34;
From what I can discern from the documentation, this is the correct way of declaring an object, and why is it just the first variable is being flagged?