mysql - Formatting json to geojson via PHP -


i trying data have called mysql database correctly display in geojson format. here's of php code:

$data = array(); //setting empty php array data go  if($result = mysqli_query($db,$query)) {   while ($row = mysqli_fetch_assoc($result))   {     $data[] = $row;   } }  $jsondata =json_encode($data); $original_data = json_decode($jsondata, true); $coordinates = array(); foreach($original_data $key => $value) {     $coordinates[] = array($value['latitude'], $value['longitude']); } $new_data = array(     'type' => 'featurecollection',     'features' => array(array(         'type' => 'feature',     'properties' => array('time' => $value['time']),     'geometry' => array('type' => 'point', 'coordinates' => $coordinates),     ),     ), );  $final_data = json_encode($new_data, json_pretty_print); print_r($final_data); 

i've managed results far: enter image description here

but need them every set of coordinates has own "type" , "properties" key-value pair:

enter image description here

i've found issue here, can't manage on last formatting hurdle...

instead of building coordinates, need build features:

$data = array(); //setting empty php array data go  if($result = mysqli_query($db,$query)) {   while ($row = mysqli_fetch_assoc($result))   {     $data[] = $row;   } }  $jsondata =json_encode($data); $original_data = json_decode($jsondata, true); $features = array(); foreach($original_data $key => $value) {     $features[] = array(         'type' => 'feature',         'properties' => array('time' => $value['time']),         'geometry' => array(              'type' => 'point',               'coordinates' => array(                   $value['latitude'],                    $value['longitude'],                    1              ),          ),     ); } $new_data = array(     'type' => 'featurecollection',     'features' => $features, );  $final_data = json_encode($new_data, json_pretty_print); print_r($final_data); 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -