PHP 数组 JSON 没有 []
PHP Arrays to JSON without []
我正在使用 CMS,想将一些 PHP 数组传递给 JSON。我设法做得很好,但问题是它包裹了 [] 数组,而我需要它而不用 [] 将它用作 Mapbox 中的 geojson (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"}}]
更新
我想出了以下方法,它在格式化方面有效,但它只返回一项而不是三项?
$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(
然后它 returns 所有项目但它有正方形上面有括号。
我陷入了兔子洞的深处...
请先将数组转换为对象,然后再将其编码为 json,这样应删除 []。希望对您有所帮助!!
从变量 $programme_array[] 中删除 [],因此它应该看起来像 $programme_array = array(......................);
并且它会完成工作。事实上,你不需要说 $programme_array[] 因为你已经将它初始化为 $programme_array = array().
您需要在适当的地方而不是在需要对象的地方使用数组。当您需要对象时,您可以使用 stdClass
。以下是如何创建一个 PHP 结构,完美编码为 JSON:
<?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;
@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"}}}
这是我经过多次试验和错误后得出的结论。
<?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);
?>
在没有它们的 json 中获得 [] 的最简单方法是这样做:
//$json 是你的 json 没有 []
//$data 一个空数组
$数据[]=$json;
print_r($数据); // 你的 $data 现在是 json with []
我正在使用 CMS,想将一些 PHP 数组传递给 JSON。我设法做得很好,但问题是它包裹了 [] 数组,而我需要它而不用 [] 将它用作 Mapbox 中的 geojson (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"}}]
更新
我想出了以下方法,它在格式化方面有效,但它只返回一项而不是三项?
$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(
然后它 returns 所有项目但它有正方形上面有括号。
我陷入了兔子洞的深处...
请先将数组转换为对象,然后再将其编码为 json,这样应删除 []。希望对您有所帮助!!
从变量 $programme_array[] 中删除 [],因此它应该看起来像 $programme_array = array(......................);
并且它会完成工作。事实上,你不需要说 $programme_array[] 因为你已经将它初始化为 $programme_array = array().
您需要在适当的地方而不是在需要对象的地方使用数组。当您需要对象时,您可以使用 stdClass
。以下是如何创建一个 PHP 结构,完美编码为 JSON:
<?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;
@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"}}}
这是我经过多次试验和错误后得出的结论。
<?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);
?>
在没有它们的 json 中获得 [] 的最简单方法是这样做:
//$json 是你的 json 没有 [] //$data 一个空数组
$数据[]=$json; print_r($数据); // 你的 $data 现在是 json with []