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

我只是一个虾纸丫 提交于 2019-12-08 05:46:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!