2015-05-13 168 views
2

我從PHP輸出一些JSON,但我有困難,瞭解如何嵌套數組(至少我認爲這是它叫什麼)JSON從PHP - 嵌套數組

我可以輸出單集,例如,"type": "Feature"但我會怎麼做

"geometry": { 
    "type": "Point", 
    "coordinates": [-77.03238901390978,38.913188059745586] 
}, 

例如,對於一個項目的JSON數組中所需的輸出可能是:

{ 
    "type": "Feature", 
    "geometry": { 
     "type": "Point", 
     "coordinates": [-77.03238901390978,38.913188059745586] 
    }, 
    "properties": { 
     "title": "Mapbox DC", 
     "description": "1714 14th St NW, Washington DC", 
     "marker-color": "#fc4353", 
     "marker-size": "large", 
     "marker-symbol": "monument" 
    } 
}, 

而且到目前爲止我的代碼看起來升IKE此:

<?php 
$projects = $pages->find('template=project-detail, sort=sort'); 
$projects_array = array(); 

foreach ($projects as $project) { 

    $title = $project->title; 
    $long = $project->project_location_marker_long; 
    $lat = $project->project_location_marker_lat; 

    $projects_array[] = array(
     'title' => $title 
    ); 

} 

$projects_json = json_encode($projects_array, true); 
?> 
<script> 
var geojson = <?php echo echo $projects_json; ?> 
</script> 

產生類似如下:

[{ 
    "title": "Steel Strike 1980" 
}, { 
    "title": "Chapel Flat Dyke Boat" 
}] 
+0

爲什麼你將'true'作爲'json_encode'的第二個參數?該函數的第二個參數是一個被常量填充的選項。有關更多信息,請參閱http://php.net/manual/en/function.json-encode.php。 也許你正在考慮'json_decode',它需要第二個參數來返回一個關聯數組。 –

回答

3

嵌套陣列是簡單的創建。這裏有一個例子:

$my_array = array(
    'string_example' => 'asdf', 
    'integer_example' => 42, 
    'array_example' => array() // this array is nested 
); 

在這個嵌套數組中,你可以放任何你想要的東西。例如,讓我們把同樣的事情在裏面:

$my_array = array(
    'string_example' => 'asdf', 
    'integer_example' => 42, 
    'array_example' => array(
     'string_example' => 'asdf', 
     'integer_example' => 42, 
     'array_example' => array() 
    ) 
); 

所以從您的代碼示例的工作,這裏是一個開始,給你包含在數據:

foreach ($projects as $project) { 

    $title = $project->title; 
    $long = $project->project_location_marker_long; 
    $lat = $project->project_location_marker_lat; 

    $projects_array[] = array(
     'geometry' => array(
      'coordinates' => array($long, $lat) 
     ) 
     'properties' => array(
      'title' => $title 
     ) 
    ); 

} 

這將導致以下json時編碼:

{ 
    "geometry": { 
     "coordinates": [-77.03238901390978,38.913188059745586] 
    }, 
    "properties": { 
     "title": "Mapbox DC", 
    } 
} 
0

有一個簡單的方法來解決這個問題。只要看看你的榜樣JSON,對其進行解碼並查看輸出結果是什麼樣子:

<?php 
$json = ' 
{ 
    "type": "Feature", 
    "geometry": { 
     "type": "Point", 
     "coordinates": [-77.03238901390978,38.913188059745586] 
    }, 
    "properties": { 
     "title": "Mapbox DC", 
     "description": "1714 14th St NW, Washington DC", 
     "marker-color": "#fc4353", 
     "marker-size": "large", 
     "marker-symbol": "monument" 
    } 
}'; 
var_export(json_decode($json, true)); 

輸出:

array (
    'type' => 'Feature', 
    'geometry' => 
    array (
    'type' => 'Point', 
    'coordinates' => 
    array (
     0 => -77.032389013909778, 
     1 => 38.913188059745586, 
    ), 
), 
    'properties' => 
    array (
    'title' => 'Mapbox DC', 
    'description' => '1714 14th St NW, Washington DC', 
    'marker-color' => '#fc4353', 
    'marker-size' => 'large', 
    'marker-symbol' => 'monument', 
), 
) 
0

如果你喜歡編碼例如中緯度/經度示例代碼這將是:

$title = $project->title; 
$long = $project->project_location_marker_long; 
$lat = $project->project_location_marker_lat; 

$projects_array[] = array(
    'title' => $title, 
    'coordinates' => array($lat,$lon) 
); 

這將導致這樣的事情:

[{ 
    "title": "Steel Strike 1980", 
    "coordinates": [-77.03238901390978,38.913188059745586] 
}, { 
    "title": "Chapel Flat Dyke Boat", 
    "coordinates": [-77.03238901390978,38.913188059745586] 
}]