PHP数组转换为JSON,不包含[]

问题描述 投票:-1回答:5

我正在使用CMS,并希望将一些PHP数组传递给JSON。我正在设法做到这一点,但是问题在于它将[]包裹在数组周围,而我需要[[]来将其用作Mapbox(https://www.mapbox.com/mapbox-gl-js/example/popup-on-click/)中的geojson。

<?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

我想出了以下内容,它可以格式化但只返回一项而不是三项?

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

甚至更奇怪的是,如果我将方括号[ ]添加到$marker = array(有效地使其设为$marker[] = array(,那么它将返回所有带有方括号的项目。

我在兔子洞里很深...

javascript php json mapbox geojson
5个回答
0
投票

[在将数组编码为json之前,请先将数组转换为对象,然后删除[]。希望对您有帮助!

从变量$ programme_array []中删除[],因此它看起来应该像$programme_array = array(......................);,它将完成工作。实际上,您不必将$ programme_array []初始化为$ programme_array = array()。


0
投票

您需要在适当的地方使用数组,而不是在需要对象的地方。需要对象时,可以使用stdClass。这是创建完美编码为JSON的PHP结构的方法:

<?php

$feature = new stdClass();
$feature->type = 'Feature';
$feature->properties = new stdClass();
$feature->properties->key = 'value';
$feature->geometry = new stdClass();
$feature->geometry->type = 'Point';
$feature->geometry->coordinates = array(0,0);

$json = json_encode($feature);

$json保留:

"{"type":"Feature","properties":{"key":"value"},"geometry":{"type":"Point","coordinates":[0,0]}}"

[当您将其发送到客户端并用Javascript解析时,它变成:

{
    "type":"Feature",
    "properties": {
         "key":"value"
    },
    "geometry": {
        "type":"Point",
        "coordinates":[0,0]
    }
}

因此,当您创建GeoJSON集合对象时:

<?php

$collection = new stdClass();
$collection->type = 'FeatureCollection';
$collection->features = array();

您可以将$feature推入$collection->features

$collection->features[] = $feature;

0
投票

@ braw,请检查以下代码。

$programmes = array('1', '2');
$programme_array = array();
foreach ($programmes as $programme) {

    $programme_array[] = array(
        'type' => 'Feature',
        'geometry' => array(
            'type' => 'Point',
            'coordinates' => ['$long', '$lat']
        ),
        'properties' => array(
            'title' => '$title',
            'description' => '$description',
            'image' => '$image',
            'url' => '$url',
            "marker-symbol" => "music"
        ),
    );
}

在这里输出:

Array
(
    [0] => Array
        (
            [type] => Feature
            [geometry] => Array
                (
                    [type] => Point
                    [coordinates] => Array
                        (
                            [0] => $long
                            [1] => $lat
                        )

                )

            [properties] => Array
                (
                    [title] => $title
                    [description] => $description
                    [image] => $image
                    [url] => $url
                    [marker-symbol] => music
                )

        )

    [1] => Array
        (
            [type] => Feature
            [geometry] => Array
                (
                    [type] => Point
                    [coordinates] => Array
                        (
                            [0] => $long
                            [1] => $lat
                        )

                )

            [properties] => Array
                (
                    [title] => $title
                    [description] => $description
                    [image] => $image
                    [url] => $url
                    [marker-symbol] => music
                )

        )

) 

运行以下代码,查看不带[]的json输出。

echo json_encode( (object) $programme_array ); 

输出:

{"0":{"type":"Feature","geometry":{"type":"Point","coordinates":["$long","$lat"]},"properties":{"title":"$title","description":"$description","image":"$image","url":"$url","marker-symbol":"music"}},"1":{"type":"Feature","geometry":{"type":"Point","coordinates":["$long","$lat"]},"properties":{"title":"$title","description":"$description","image":"$image","url":"$url","marker-symbol":"music"}}}

0
投票

这是经过反复尝试后最终得到的结果。

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

?>

0
投票

没有它们的情况下在json中获取[]的最简单方法是这样做:

// $ json是不带[]的json// $ data一个空数组

$ data [] = $ json;print_r($ data); //您的$ data现在是带有[]

的json
© www.soinside.com 2019 - 2024. All rights reserved.