问题
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 !
回答1:
Thats because $coordinates
is of type String.
$coordinates="[".$row["lat"].",".$row["lng"]."]";
Create $coordinates
like this
$coordinates = array($row["lat"],$row["lng"]);
来源:https://stackoverflow.com/questions/11012695/php-array-to-json-how-to-get-rid-of-some-double-quotes