0

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!

0

5 Answers 5

3

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
  • don't forget that all of the data just be assigned to a single property in the main object: $data = array( 'data' => array('type' => $type));... Commented Dec 15, 2014 at 13:44
  • 1
    The text as it displayed in the question (starting with 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. Commented Dec 15, 2014 at 13:49
0

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
  • Thank you for the reply!! That's good.. I did got that working.. now what I've to do it's to remove those quotes in data.. and remote the {} at the beggining. I tried many workarounds.. but I can't get it to work.. I hate PHP haha. Any idea how can I do that? thank you! Commented Dec 15, 2014 at 14:09
  • Then try this: $json_string = substr(ltrim(preg_replace('/"/', '', json_encode(array('data' => $data))), "{"), 0, -1); Commented Dec 15, 2014 at 14:13
  • awesome!!! thank you.. something I didn't say is that I load more than just one array... and with this code.. I mean without usin the [].. it will just load 1... what I'm doing wrong? Commented Dec 15, 2014 at 14:29
  • Then you need to add your elements to an array line by line. You can not do this in 1 step. Commented Dec 15, 2014 at 14:30
0

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;
0

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.

0

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
  • that gives you back {"data":[{"id":1,"type":"type","coordinates":[42.565,19.6464],"colour":null,"route":"Route"}]} Commented Dec 15, 2014 at 13:33
  • Thank you for reply! now this is what I've {"data":[{"type":"Feature","geometry":["-34.66159","-59.42428"],"color":"#FFFFFF","route":"u", id: "118"}]} I need to remove those [] that prints after "data" and those {} and the beggining.. I tried str_replace but it won't work correctly Commented Dec 15, 2014 at 13:37
  • @JoshChisnerman: Cf all of the other answers here: this answer is probably the wors of all, because of $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 Commented Dec 15, 2014 at 13:42
  • Sorry, didn't notice the extra brackets there, therefore I edited my answer Commented Dec 15, 2014 at 13:44
  • Thank you again for replying.. I just have to little issues.. hope you can help me with those. 1st, it starts with {"data":{... and I needed like this data: {.. without the {} and the beggining and without the quotes.. is that possible? thank you again! Commented Dec 15, 2014 at 13:55

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.