Olivier
Olivier

Reputation: 54

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

I have a small but weird problem while encoding php arrays to json.

I need to prevent array() from adding double quotes around specific values.

Here is the php array:

$coordinates="[".$row["lat"].",".$row["lng"]."]";  
$egUser=array(              

            "geometry"=>array(
                "type"=>"$type",
                "coordinates"=>$coordinates                 
        ),

            "type2"=>"$type2",
            "id"=>$id
        );
$arrayjson[]=$egUser;   

Wich return the following json with json_encode :

var member = {
"type": "FeatureCollection",
"features": [{
    "geometry": {
        "type": "Point",
        "coordinates": "[46.004028,5.040131]"
    },
    "type2": "Feature",
    "id": "39740"
}]

};

As you can see the coordinates are wrapped inside double quote >

"coordinates": "[46.004028,5.040131]"

How do I get rid of these quotes ? I need to have the following instead >

"coordinates": [46.004028,5.040131]

I'm a bit confuse so any help is welcome :) Thank you !

Upvotes: 1

Views: 1777

Answers (1)

Josnidhin
Josnidhin

Reputation: 12504

Thats because $coordinates is of type String.

$coordinates="[".$row["lat"].",".$row["lng"]."]";

Create $coordinates like this

$coordinates = array($row["lat"],$row["lng"]);

Upvotes: 3

Related Questions