this is a simple question.. I'm trying to use leaflet realtime library, but it requires to load a specific JSON output such as:
data: {"type":"Feature","geometry":{"type":"Point","coordinates":[-85.26995166666667,35.056891]},"properties":{"color":"#FFFFFF","route":"U"},"id":"118"}
so far this is all I got:
[{"type":"Feature","coordinates":["-34.66159","-59.42428"],"color":"#FFFFFF","route":"u", id:118}]
this is my PHP
$id=$row['id'];
$type=$row['type'];
$lat=$row['lat'];
$lng=$row['lng'];
$color=$row['color'];
$route=$row['route'];
$data [] = array('id'=> $id, 'type'=> $type,'coordinates'=> $lnglat = array ($lat, $lng),
'colour'=> $colour, 'route'=> $route);
$json_string = json_encode($data);
echo $json_string;
This is driving crazy... I tried everything.. I read lots of json_encode tutorial..but I can't find an example or situation such as mine.
Please, help!
Thank you!
5 Answers 5
Try to decode the json you need to create to see how the PHP data structure should look like:
$json='{"type":"Feature","geometry":{"type":"Point","coordinates":[-85.26995166666667,35.056891]},"properties":{"color":"#FFFFFF","route":"U"},"id":"118"}';
$data = json_decode($json, TRUE);
print_r($data);
The output is:
Array
(
[type] => Feature
[geometry] => Array
(
[type] => Point
[coordinates] => Array
(
[0] => -85.269951666667
[1] => 35.056891
)
)
[properties] => Array
(
[color] => #FFFFFF
[route] => U
)
[id] => 118
)
Now, your code should look like:
$data = array(
'type' => $type,
'geometry' => array(
'type' => 'Point',
'coordinates' => array($lat, $lng),
),
'properties' => array(
'color' => $color,
'route' => $route,
),
'id' => $id,
);
echo(json_encode($data));
UPDATE: as @paul-crovella remarks in a comment, using var_export() instead of print_r() in the first fragment of code produces directly PHP code very similar with the one displayed above (except for the variables/values) that can be copy-pasted into your code and used by replacing the values ('Features', 35.056891, '#FFFFFF' a.s.o.) with the corresponding variables ($type, $lng, $color etc).
2 Comments
$data = array( 'data' => array('type' => $type));...data: ) is not valid JSON. I assumed data is the field name that must contain the json-encoded information. It is not part of the data but part of the API.You can try this:
$id = 1;
$type = "type";
$lat = 42.565;
$lng = 19.6464;
$colour = "Color";
$route = "Route";
$data = array('id' => $id, 'type' => $type, 'coordinates' => $lnglat = array($lat, $lng),
'colour' => $colour, 'route' => $route);
$json_string = json_encode(array('data' => $data));
echo $json_string;
Remove the [] from $data.
Output is:
{"data":{"id":1,"type":"type","coordinates":[42.565,19.6464],"colour":"Color","route":"Route"}}
4 Comments
$json_string = substr(ltrim(preg_replace('/"/', '', json_encode(array('data' => $data))), "{"), 0, -1);This will work for you:
$dataArray = array('id'=> $id, 'type'=> $type,'coordinates'=> $lnglat = array ($lat, $lng), 'colour'=> $colour, 'route'=> $route);
$json_string = json_encode(array('data'=>$dataArray));
echo $json_string;
Comments
Your assignment to the $data array should look like this:
$data['data'] = array(
'id' => $id,
'type' => $type,
'coordinates' => $lnglat = array($lat, $lng),
'colour' => $color,
'route' => $route
);
P.S. You also had a typo in the $color variable.
Comments
Did you try this:
$id=$row['id'];
$type=$row['type'];
$lat=$row['lat'];
$lng=$row['lng'];
$color=$row['color'];
$route=$row['route'];
$data [] = array('id'=> $id, 'type'=> $type,'coordinates'=> $lnglat = array ($lat, $lng),
'colour'=> $colour, 'route'=> $route);
$json_string = json_encode(array('data'=>$data));
echo $json_string;
All I did was adding one more level into array():
$json_string = json_encode($data);
Into this:
$json_string = json_encode(array('data'=>$data));
Edit:
$data['data'] = array('id'=> $id, 'type'=> $type,'coordinates'=> $lnglat = array ($lat, $lng),
'colour'=> $colour, 'route'=> $route);
$json_string = json_encode($data);
echo $json_string;
OR
$data = array('id'=> $id, 'type'=> $type,'coordinates'=> $lnglat = array ($lat, $lng),
'colour'=> $colour, 'route'=> $route);
$json_string = json_encode(array('data'=>$data));
echo $json_string;
5 Comments
{"data":[{"id":1,"type":"type","coordinates":[42.565,19.6464],"colour":null,"route":"Route"}]} $data[] = array() <-- this is what causes the square brackets in the JSON string. I explain why they're there in my answer. The simple answer is to replace $data[] = array(); with $data = array(); all of the other answers tell you the same thing