doujiu3882 2016-02-23 11:44
浏览 59
已采纳

没有[]的PHP数组到JSON

I am using a CMS, and want to pass some PHP arrays to JSON. I am managing to do this fine but the issue is that it wraps [] round the array, whereas I need it without [] to use it as geojson within Mapbox (https://www.mapbox.com/mapbox-gl-js/example/popup-on-click/).

<?php 
$programme_array = array();
$programmes = $pages->find('parent=programme, sort=sort');
foreach ($programmes as $programme) {
    $title = $programme->title;
    $url = $programme->url;
    $summary = $programme->programme_summary;
    $image = $programme->programme_venue_image->url;
    $long = $programme->programme_location->lng;
    $lat = $programme->programme_location->lat;
    $programme_array[] = array(
        'type' => 'Feature',
        'geometry' => array(
            'type' => 'Point',
            'coordinates' => [$long,$lat]
        ),
        'properties' => array(
            'title' => $title,
            'description' => $summary,
            'image' => $image,
            'url' => $url,
            "marker-symbol" => "music"
        ),
    );

}
$programme_json = json_encode($programme_array, true);
?>

[{"type":"Feature","geometry":{"type":"Point","coordinates":["-1.466439","53.376842"]},"properties":{"title":"Site Gallery","description":"Duis arcu tortor, suscipit eget, imperdiet nec, imperdiet iaculis, ipsum. Donec id justo. Aenean tellus metus, bibendum sed, posuere ac, mattis non, nunc. Suspendisse feugiat. Etiam rhoncus.","image":"\/Freelance\/art-sheffield-2016\/site\/assets\/files\/1032\/site_gallery.jpg","url":"\/Freelance\/art-sheffield-2016\/programme\/site-gallery\/","marker-symbol":"music"}},{"type":"Feature","geometry":{"type":"Point","coordinates":["-1.477881","53.374798"]},"properties":{"title":"Moore Street Substation","description":"","image":null,"url":"\/Freelance\/art-sheffield-2016\/programme\/moore-street-substation\/","marker-symbol":"music"}},{"type":"Feature","geometry":{"type":"Point","coordinates":["-1.459620","53.380562"]},"properties":{"title":"S1 Artspace","description":"","image":null,"url":"\/Freelance\/art-sheffield-2016\/programme\/s1-artspace\/","marker-symbol":"music"}}]

UPDATE

I have come up with the following, which works in terms of formatting BUT it's only returning one item and not three?

$geojson = array( 'type' => 'FeatureCollection', 'features' => array());

$programmes = $pages->find('parent=programme, sort=sort');
foreach ($programmes as $programme) {

  $marker = array(
    'type' => 'Feature',
    'properties' => array(
      'title' => $programme->title,
      "marker-symbol" => "music"
    ),
    'geometry' => array(
      'type' => 'Point',
      'coordinates' => array( 
        $programme->programme_location->lng,
        $programme->programme_location->lat
      )
    )
  );
  array_push($geojson['features'], $marker);
}
$programme_json = json_encode($marker, JSON_PRETTY_PRINT);

What's even weirder is that if I add the square brackets [ ] to $marker = array( effectively making it $marker[] = array( then it returns all the items BUT it has the square brackets on it.

I'm so deep down the rabbit hole...

  • 写回答

4条回答 默认 最新

  • douyijin7741 2016-02-23 19:25
    关注

    This is what I ended up with after much trial and error.

    <?php 
    
    $geojson = array(
        'type' => 'FeatureCollection', 
        'features' => array()
    );
    
    $programmes = $pages->find('parent=programme, sort=sort');
    foreach ($programmes as $programme) {
    
        $marker = array(
            'type' => 'Feature',
            'properties' => array(
                'title' => $programme->title,
                'url' => $programme->url,
                'summary' => $programme->programme_summary,
                'image' => $programme->programme_venue_image->url
            ),
            'geometry' => array(
                'type' => 'Point',
                'coordinates' => array( 
                    $programme->programme_location->lng,
                    $programme->programme_location->lat
                )
            )
        );
        array_push($geojson['features'], $marker);
    
    }
    $programme_json = json_encode($geojson);
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿