-1

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.

Community
  • 1
  • 1
Kuntal Parbat
  • 159
  • 1
  • 2
  • 12

2 Answers2

0

@Simba - Thank you for guide me. Your guides help me to get exact result

$output = json_decode('['.$row['more_info'].']');
$feature = array(
        'type' => 'Feature',
         'properties' => array(
             'score' => "",
             'fid' => ""
        //Other fields here, end without a comma
            ),

      'geometry' => array(
        'type' => $type,
        'coordinates' => array($output)
            )
        );
Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
Kuntal Parbat
  • 159
  • 1
  • 2
  • 12
  • Relax, there's only one negative vote. Maybe you just caught someone having a bad day. But complaining about it usually makes things worse. – Simba Oct 19 '16 at 11:08
0

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)
Simba
  • 4,952
  • 3
  • 19
  • 29
  • $row['more_info'] is contanning polygon value [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] – Kuntal Parbat Oct 19 '16 at 11:23
  • okay, so you need to drop the `` and ``, and then to the `json_decode()` as per the answer. – Simba Oct 19 '16 at 11:27
  • No, I only need to it to print exact it was [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] With [ ] before after as it output [[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]] But I do not want Double quotation before and after – Kuntal Parbat Oct 19 '16 at 11:31
  • and is nothing, I put it to show output only – Kuntal Parbat Oct 19 '16 at 11:33
  • Use backtick characters to show code like that on this site. :) – Simba Oct 19 '16 at 11:57
  • 1
    Okay, so if `$row['more_info']` contains all of that as a string, then yes, the answer is to do a json_decode() on it when adding it to the array. That will mean it should come out correct when you do the `json_encode()` at the end of the program. – Simba Oct 19 '16 at 11:58
  • you Right. I decode it to array then encode which solve my problem. Many Many thanks you are rally great. $output = json_decode('['.$row['more_info'].']'); $feature = array( 'type' => 'Feature', 'properties' => array( 'score' => "", 'fid' => "" //Other fields here, end without a comma ), 'geometry' => array( 'type' => $type, 'coordinates' => array($output) ) ); – Kuntal Parbat Oct 19 '16 at 12:13