代码之家  ›  专栏  ›  技术社区  ›  joshmoto

json编码的php数组,在坐标变量输出周围加引号

  •  1
  • joshmoto  · 技术社区  · 7 年前

    我正在编写一个wordpress循环,它获取多个post并在基于php的数组中输出post自定义字段GeoJson coordinates数组。

    $sFeatureCoordinates 变量。

      [
        [
          -0.7332730293273926,
          51.89886634382943
        ],
        [
          -0.7326534390449524,
          51.89770778622719
        ],
        [
          -0.7318434119224548,
          51.898079355455344
        ],
        [
          -0.7332730293273926,
          51.89886634382943
        ]
      ]
    

    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);
    

    下面是从上面的数组输出的json示例 http://myjson.com/pssis

    我的问题是 coordinates 当它被编码的时候会被用引号括起来。

    有什么办法能阻止我的行为吗 将内容用引号括起来 json\ U编码

    非常感谢

    1 回复  |  直到 7 年前
        1
  •  2
  •   AbraCadaver    7 年前

    $sFeatureCoordinates 是JSON,所以只需解码它并添加到数组中。然后当你 json_encode . 添加或删除 [ ]

    'coordinates' => [ [ json_decode($sFeatureCoordinates, true) ] ]
    
    推荐文章