数组结构帮助 PHP 生成 JSON


array structure help in php to generate json

从mySQL获取数据并通过php生成json,如下所示:

while ($row = mysql_fetch_array($result)) {
     $menu[] = array("type"=>"FeatureCollection", 
                    "features" => [array("type"=>"Feature",
                                         "geometry"=>array("type"=>"Point",
                                                           "coordinates"=>[-77.034084142948,38.909671288923]),
                                         "properties"=>array("phone"=>"041","city"=>"Faisalabad","country"=>"Pakistan"))]       
        );
}   $json =  json_encode($menu); 
    echo $json;

结果如下所示:

[
{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -77.034084142948,
                    38.909671288923
                ]
            },
            "properties": {
                "phone": "041",
                "city": "Faisalabad",
                "country": "Pakistan"
            }
        }
    ]
},
{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -77.034084142948,
                    38.909671288923
                ]
            },
            "properties": {
                "phone": "041",
                "city": "Faisalabad",
                "country": "Pakistan"
            }
        }
    ]
},
{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -77.034084142948,
                    38.909671288923
                ]
            },
            "properties": {
                "phone": "041",
                "city": "Faisalabad",
                "country": "Pakistan"
            }
        }
    ]
}]

如您所见{"type": "FeatureCollection","features":[{...}]}重复了三次,我希望它只像下面的json一样出现在开头,以便{"type":"FeatureCollection","features":[{我只想在这里循环}]}:我想要这个结果:

[
{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -77.034084142948,
                    38.909671288923
                ]
            },
            "properties": {
                "phone": "041",
                "city": "Faisalabad",
                "country": "Pakistan"
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -77.034084142948,
                    38.909671288923
                ]
            },
            "properties": {
                "phone": "041",
                "city": "Faisalabad",
                "country": "Pakistan"
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -77.034084142948,
                    38.909671288923
                ]
            },
            "properties": {
                "phone": "041",
                "city": "Faisalabad",
                "country": "Pakistan"
            }
        }
    ]
}]

请帮助我已经尝试太多了。谢谢

好吧,如果你只想要一次,为什么还要在while循环中重复"FeatureCollection"呢?

我想你想要这样的东西:

$menu = array();
features = array();
$menu['type'] = 'FeatureCollection';
while ($row = mysql_fetch_array($result)) {
     $features[] = array("type"=>"Feature",
                       //Other features here
      );
}
$menu['features'] = $features;

$menu数组中,您应该定义具有值 FeatureCollection 和键features的键type。在你的 while 循环中这样做:

$menu = array(
    'type' => 'FeatureCollection',
    'features' => array()
);
while ($row = mysql_fetch_array($result)) {
    $menu[`features`][] = array(...);
}