Search code examples
phparraysjsondouble-quotes

double quotas appearing after json output in PHP array to JSON


I have read a question answer at

PHP Array to json, how to get rid of some double quotes?

I have same issue but my code is little different so I unable to ride of rid of some double quotes.

My code :

$features = array();
$geojson = array(
    'type'      => 'FeatureCollection',
    'features'  => $features
 );
while($row = mysqli_fetch_assoc($result)) {
$type = $row['type'];
if($type == 'Point')
    {
        //$output = ;
            $feature = array(
        'type' => 'Feature',
         'properties' => array(
             'score' => "",
             'fid' => ""
        //Other fields here, end without a comma
            ),

      'geometry' => array(
        'type' => $type,
        'coordinates' => array($row['lat'], $row['lng'])
            )
        );
    }
else {
//$output = '['.$row['more_info'].']';
$feature = array(
        'type' => 'Feature',
         'properties' => array(
             'score' => "",
             'fid' => ""
        //Other fields here, end without a comma
            ),

      'geometry' => array(
        'type' => $type,
        'coordinates' => array('['.$row['more_info'].']')
            )
        );
}

    array_push($geojson['features'], $feature);
};
    mysqli_close($conn);
    echo $newgeojson =  json_encode($geojson, JSON_NUMERIC_CHECK);

array Output before convert to json (json_encode):

Array
(
    [type] => FeatureCollection
    [features] => Array
        (
            [0] => Array
                (
                    [type] => Feature
                    [properties] => Array
                        (
                            [score] => 
                            [fid] => 
                        )

                    [geometry] => Array
                        (
                            [type] => Point
                            [coordinates] => Array
                                (
                                    [0] => 88.388786315918
                                    [1] => 22.551879205144
                                )

                        )

                )

            [1] => Array
                (
                    [type] => Feature
                    [properties] => Array
                        (
                            [score] => 
                            [fid] => 
                        )

                    [geometry] => Array
                        (
                            [type] => Polygon
                            [coordinates] => Array
                                (
                                    [0] => [[88.41796875, 22.568048815726],[88.41796875, 22.572804222704],[88.41796875, 22.577876475976],[88.428955078125, 22.578114233267],[88.428955078125, 22.573121244003],[88.428611755371, 22.568048815726],[88.41796875, 22.568048815726]]
                                )

                        )

                )

        )

)

Output I am getting after json_encode

{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"score":"","fid":""},"geometry":{"type":"Point","coordinates":[88.388786315918,22.551879205144]}},{"type":"Feature","properties":{"score":"","fid":""},"geometry":{"type":"Polygon","coordinates":["[[88.41796875, 22.568048815726],[88.41796875, 22.572804222704],[88.41796875, 22.577876475976],[88.428955078125, 22.578114233267],[88.428955078125, 22.573121244003],[88.428611755371, 22.568048815726],[88.41796875, 22.568048815726]]"]}}]}

As you can see the coordinates coming with " ".

"[[88.41796875, 22.568048815726],[88.41796875, 22.572804222704],[88.41796875, 22.577876475976],[88.428955078125, 22.578114233267],[88.428955078125, 22.573121244003],[88.428611755371, 22.568048815726],[88.41796875, 22.568048815726]]"

I do not want that double quote start and end of multi coordinates.

Please guide / suggest me how can I ride of those double quote.


Solution

  • The problem is here: 'coordinates' => array('['.$row['more_info'].']').

    I don't know why you are adding '[' and ']' here, but it looks like you're trying to build the JSON string manually. If you do that, then yes, you will end up with unwanted quotes when you use json_encode() because json_encode() treats the entire contents as a string.

    It's not clear what $row['more_info'] contains to start with, but I guess it contains a string that is already in JSON format. If you want to add this to your output JSON, then the first thing you need to do is convert it back into a PHP array.

    Your code should probably look like this:

    'coordinates' => json_decode($row['more_info'], true)