hfarazm
hfarazm

Reputation: 1417

array structure help in php to generate json

Fetching data from mySQL and Generating json through php like this:

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;

and the result looks like this:

[
{
    "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"
            }
        }
    ]
}]

As you can see {"type": "FeatureCollection","features":[{...}]}is being repeated three time, I want it to appear at the beginning only like following json, so that {"type": "FeatureCollection","features":[{I WANT LOOP HERE only}]}: I want this result:

[
{
    "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"
            }
        }
    ]
}]

Please help I have been trying too much. Thanks

Upvotes: 0

Views: 91

Answers (2)

u_mulder
u_mulder

Reputation: 54831

In your $menu array you shoud define key type with value FeatureCollection, and key features. And in your while loop do this:

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

Upvotes: 1

DJWLaan
DJWLaan

Reputation: 36

Well, why do you keep repeating "FeatureCollection" in the while-loop if you only want it once?

I think you want something like this:

$menu = array();
features = array();
$menu['type'] = 'FeatureCollection';

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

$menu['features'] = $features;

Upvotes: 1

Related Questions