php - json编码的php数组在坐标变量输出周围环绕引号
问题描述
我正在编写一个 wordpress 循环,它获取多个帖子并在基于 php 的数组中输出帖子自定义字段 GeoJson 坐标数组。
请参阅下面的$sFeatureCoordinates
变量内容。
[
[
-0.7332730293273926,
51.89886634382943
],
[
-0.7326534390449524,
51.89770778622719
],
[
-0.7318434119224548,
51.898079355455344
],
[
-0.7332730293273926,
51.89886634382943
]
]
然后看下面我的 php 数组,然后我用它json_encode
来格式化数组。
$arr = [
'type' => 'FeatureCollection',
'features' => []
];
// loop through each post
while($oQuery->have_posts()): $oQuery->the_post();
// grab our type and coordinates
$sFeatureType = get_field('geojson_feature_type');
$sFeatureCoordinates = get_field('geojson_feature_coordinates');
// add this to our features
$arr['features'][] = [
'type' => 'Feature',
'properties' => [
'id' => get_the_id(),
'name' => get_the_title()
],
'geometry' => [
'type' => $sFeatureType,
'coordinates' => [
[ preg_replace('/\s+/', '', $sFeatureCoordinates) ]
]
]
];
endwhile;
$json = json_encode($arr);
这是从http://myjson.com/pssis上面的数组输出的我的 json 示例
我遇到的问题是,coordinates
当它被编码时,它被包裹在引号中。
当我的谷歌地图脚本尝试读取它时,这会导致 js 错误。
关于如何阻止我的coordinates
内容被引号包裹的任何想法json_encode
?
非常感谢
解决方案
$sFeatureCoordinates
是 JSON,所以只需将其解码并添加到数组中。然后,当您json_encode
. 根据需要添加或删除[ ]
以获得正确的嵌套级别:
'coordinates' => [ [ json_decode($sFeatureCoordinates, true) ] ]
推荐阅读
- objective-c - 带有 Coredata 的 Objective-c NSXMLParser 解析和存储数据的时间太长
- python - Python 没有名为 X 的模块 - 绝对导入
- c - 如何将结构数组从main传递到另一个函数
- php - Foreach 循环 Json 数组
- c++ - 如何在设置字符串值时优化函数调用?
- cassandra - 单集群 2 节点数据中心,用于复制测试,关闭 1 个节点,然后尝试在其他节点中运行选择查询时出错
- javascript - 网站内容在 PDF A4 页面中没有响应
- python - Python用最高优先级列表替换元组列表中的元组
- gulp - 更新到版本 4 后 Gulp 功能不起作用
- reactjs - 使用adonis在heroku中运行应用程序失败,下一步,反应应用程序框架