Home > OS >  Creating Multiple JSON Object Elements Programmatically using PHP
Creating Multiple JSON Object Elements Programmatically using PHP

Time:02-20

I need to create a JSON object, in PHP, with the following form

{
"Routes": [{
        "route_ID": "49",
        "geom": [{
            "lat": "-30.63607",
            "lng": "147.499935"
        }, {
            "lat": "-30.63631",
            "lng": "147.499868"
        }]

    },
    {
        "route_ID": "50",
        "geom": [{
            "lat": "-30.63607",
            "lng": "147.499935"
        }, {
            "lat": "-30.63631",
            "lng": "147.499868"
        }]
    }
]

}

The problem with my code is that I can't seem to find a way of concatenating the JSON elements from the $jsonString, The code as it is just produces "route_ID": "50".

(Yes I agree there are a great many questions on stackoverflow on this question, but they just about all refer to single elements derived from a static array). By the way, I am given the $jsonString and need to preprocess it in the code below.

 $jsonString = "[{\"route_ID\":\"49\",\"geom\":\"<LineString><coordinates>147.499935,-30.63607  <\\/coordinates><\\/LineString>\"},{\"route_ID\":\"50\",\"geom\":\"<LineString><coordinates>147.499935,-30.63607<\\/coordinates><\\/LineString>\"}]";

 /*  PHP Code  */
$jsonArray = json_decode($jsonString, true);
$json = new stdClass();
$json_Routes = new stdClass();
$route_geom_Array = array();

foreach ($jsonArray as $record) {
    print_r($record);
    $geomString = $record["geom"];
    $geomString = str_replace('<LineString><coordinates>', ' ', $geomString);
    $geomString = str_replace('</coordinates></LineString>', ' ', $geomString);

    $geomString = trim($geomString);
    $route_geom_Array = explode(' ', $geomString);

    for ($i = 0; $i < sizeof($route_geom_Array); $i  ) {
       $var = explode(',', $route_geom_Array[$i]);
        $route_geom[] = array('lat' => $var[1], 'lng' => $var[0]); // lat long  
    }
    $json->route_ID = $record["route_ID"];  //<- Problem area  
    $json->geom = $route_geom;              //<- Problem area
}
$json_Routes->Routes = $json;

$jsonArray = json_encode($json_Routes);
echo $jsonArray;
exit ();`

Can someone give me a clue on how do this.

Thanks

CodePudding user response:

You need to make $json_Routes->Routes an array and push the $json structures within the foreach block, e.g.:

$json_Routes->Routes = [];

foreach ($jsonArray as $record) {

  // Existing code ...
  $json_Routes->Routes[] = $json;

}

CodePudding user response:

You need to push objects to array, here solution :

$jsonArray = json_decode($jsonString, true);

$json_Routes = new stdClass();
$route_Array = [];
$route_geom_Array = [];
$route_ID_Array = [];

$items = [];
$index = 0;


foreach ($jsonArray as $record) {
    //print_r($record);
    $geomString = $record["geom"];
    $geomString = str_replace('<LineString><coordinates>', ' ', $geomString);
    $geomString = str_replace('</coordinates></LineString>', ' ', $geomString);

    $geomString = trim($geomString);
    $route_geom_Array = explode(' ', $geomString);

    for ($i = 0; $i < sizeof($route_geom_Array); $i  ) {
       $var = explode(',', $route_geom_Array[$i]);
        $route_geom[] = array('lat' => $var[1], 'lng' => $var[0]); // lat long  
    }
    $json = new stdClass();
    $json->route_ID = $record["route_ID"];  //<- Problem area  
    $json->geom = $route_geom;              //<- Problem area
    array_push($route_Array,$json);
}
$json_Routes->Routes = $route_Array;
$result = json_encode($json_Routes);
echo $result;
exit();
  • Related