gpt4 book ai didi

php - 将 PHP 数组嵌套到 json

转载 作者:行者123 更新时间:2023-12-04 05:50:09 24 4
gpt4 key购买 nike

所以我的代码在这里:

    $featurecollection = ("FeatureCollection");

$test[] = array (
"type" => $featurecollection,
$features[] = array($images)

);

file_put_contents($cache,json_encode($test));

结果为以下 json:
[
{
"type":"feature",
"0":[
[
{
"title":"some title",
"src":"value",
"lat":"value",
"lon":"value"
},
{
"title":"some title",
...

但是我需要以不同的方式嵌套,并且我对如何构造 php 数组以得到如下结果感到困惑:
{
"type":"FeatureCollection",
"features":[

{
"type":"Feature",
"geometry":{
"coordinates":[
-94.34885,
39.35757
],
"type":"Point"
},
"properties":{
"latitude":39.35757,
"title":"Kearney",
"id":919,
"description":"I REALLY need new #converse, lol. I've had these for three years. So #destroyed ! :( Oh well. Can't wait to get a new pair and put my #rainbow laces through. #gay #gaypride #bi #proud #pride #colors #shoes #allstar #supporting ",
"longitude":-94.34885,
"user":"trena1echo5",
"image":"http://images.instagram.com/media/2011/09/09/ddeb9bb508c94f2b8ff848a2d2cd3ece_7.jpg",
"instagram_id":211443415
}
},

php 数组会是什么样的?一切都是嵌套的,但仍然有一个键值,这让我很困惑。

最佳答案

这是我在 PHP 中表示的方式:

array(
'type' => 'FeatureCollection',
'features' => array(
array(
'type' => 'Feature',
'geometry' => array(
'coordinates' => array(-94.34885, 39.35757),
'type' => 'Point'
), // geometry
'properties' => array(
// latitude, longitude, id etc.
) // properties
), // end of first feature
array( ... ), // etc.
) // features
)

所以为了得到这个结构,每个特征都必须是一个关联数组:
  • 类型,
  • 几何 - 一个关联数组:
  • 坐标 - 值的索引数组,
  • 类型
  • 属性 - 纬度、经度、id 等值的关联数组。

  • 在这种时候,我更喜欢区分列表( array(1, 2, 3) )和字典或 map ( array('a' => 1, 'b' => 2) )的语言。

    关于php - 将 PHP 数组嵌套到 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10165234/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com