2

Is anyone able to give me an insight how to use PHP classes to generate a JSON structure based on the following template

{
"Summary": "A long route",
published": true,
"Route 1": [
    {
        "Lat": 23.4,
        "Long": 34.5
    },
    {
        "Lat": 27.4,
        "Long": 384.5
    } ...
]
"Route 2": [
    {
        "Lat": 25.4,
        "Long": 34.5
    },
    {
        "Lat": 29.4,
        "Long": 384.5
    } ...
]......

}

I am able to generate a constructor function that allows me to instantiate the classes using the method of $routes['Route 1'] = new Route[32.4,34.5] but I am not sure how to generate additional waypoints, or Lat and Long elements for a say Route 1, without re-instantiating the Route class.

3
  • 1
    You could create methods in your class like addWaypoint() and removeWaypoint()? Commented Aug 25, 2016 at 11:33
  • You are using json_encode() to build a json string arn't you. You are not trying to build the string manually in a text string are you Commented Aug 25, 2016 at 11:35
  • Yes, I didn't state that but it is the case. Commented Aug 25, 2016 at 11:35

2 Answers 2

1

Here is a class which will take care of your json:

<?php
Class Routes {

    private $routes = [];

    public function __construct($summary, $published){
        $this->routes['summary'] = $summary;
        $this->routes['published'] = $published;
    }

    public function addRoute($route){
        $this->routes[$route] = [];     
    }

    public function addWaypoint($route, $lat, $lon){
        $this->routes[$route][] = [
            "lat" => $lat,
            "long" => $lon
        ];
    }

    public function createJson(){
        return json_encode($this->routes);
    }

}

$routes = new Routes("A long route", true);
$routes->addRoute("Route 1");
$routes->addRoute("Route 2");
$routes->addWaypoint("Route 1", 23.4, 34.5);
$routes->addWaypoint("Route 1", 27.4, 384.5);
$routes->addWaypoint("Route 2", 25.4, 34.5);
$routes->addWaypoint("Route 2", 29.4, 384.5);

echo $routes->createJson();

Returns this JSON:

{  
   "summary":"A long route",
   "published":true,
   "Route 1":[  
      {  
         "lat":23.4,
         "long":34.5
      },
      {  
         "lat":27.4,
         "long":384.5
      }
   ],
   "Route 2":[  
      {  
         "lat":25.4,
         "long":34.5
      },
      {  
         "lat":29.4,
         "long":384.5
      }
   ]
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Daan. That is exactly what I was after. I thought I was close to getting the answer with my efforts but after seeing yours I realised that I had a way to go. I tried to vote you up, but I have such a meagre reputation it has no effect.:-( Thanks again.
1

You can do this without needing to create a formal class, but by simply using stdClass()

<?php

$json = new stdClass();
$json->summary = "A long route";
$json->published = true;

$routes = array();
$routes[] = array('Lat' => 23.4, 'Long' => 34.5);
$routes[] = array('Lat' => 27.4, 'Long' => 384.5);

$json->Route1 = $routes;

$routes = array();
$routes[] = array('Lat' => 11.4, 'Long' => 12.5);
$routes[] = array('Lat' => 12.4, 'Long' => 16.5);

$json->Route2 = $routes;

echo json_encode($json);

Output :

{
 "summary":"A long route",
 "published":true,
 "Route1":[{"Lat":23.4,"Long":34.5},
           {"Lat":27.4,"Long":384.5}
          ],
 "Route2":[{"Lat":11.4,"Long":12.5},
           {"Lat":12.4,"Long":16.5}
          ]
}

1 Comment

Very clever. You have convinced me to give up on trying to do it with class struction. Thanks

Your Answer

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