2014-06-18 2 views
0

(Laravel) 다음 : 나는 어떤 종류의 필요 알고다시 포맷 JSON 내가 laravel 출력이

{ "type": "FeatureCollection", 
    "features": [ 
     { "type": "Feature", 
     "geometry": {"type": "Point", "coordinates": [lat, lon]}, 
     "properties": { 
     "name": "value" 
     } 
     }  
     ] 
    } 

: 나는 그것이 geoJson 형식 싶습니다

[ 
{ 
"id": 3, 
"lat": "38.8978378", 
"lon": "-77.0365123" 
}, 
{ 
"id": 4, 
"lat": "44.8", 
"lon": "1.7" 
}, 
{ 
"id": 22, 
"lat": "37.59046", 
"lon": "-122.348994" 
} 
] 

고리. 하지만 PHP로 구조화하는 방법을 잘 모르겠습니다. 모든 지침은 크게 감사하겠습니다. 세계지도에 수천 개의 마커가있는지도 애플리케이션을 만들려고합니다. 이미 클러스터링에 대해 생각하고 있지만이 기본 단계를 지나야합니다.

감사합니다.

+0

이도 Laravel와 아무 상관이없는, 당신은 json_encode'와'json_decode''와 일반 PHP에서 그 작업을 수행 할 수 있습니다 사람이 관심이 있다면 함수로를 돌렸다. –

+0

맞습니다. 나는 문맥을 제공하고 있었다. Json_encode/decode는 geoJson에 대해 자동으로 형식을 지정하지 않습니다. 결국 나는 json_encode에서 루프에서 빠져 나옵니다. – aibarra

답변

2

을, 배열은 조금 떨어져이었다.

function geoJson ($locales) 
    { 
     $original_data = json_decode($locales, true); 
     $features = array(); 

     foreach($original_data as $key => $value) { 
      $features[] = array(
        'type' => 'Feature', 
        'geometry' => array('type' => 'Point', 'coordinates' => array((float)$value['lat'],(float)$value['lon'])), 
        'properties' => array('name' => $value['name'], 'id' => $value['id']), 
        ); 
      }; 

     $allfeatures = array('type' => 'FeatureCollection', 'features' => $features); 
     return json_encode($allfeatures, JSON_PRETTY_PRINT); 

    } 
1

원래 값에 json_decode()을 사용하고 새 값을 재구성/재구성하십시오. 이 예제를 고려하십시오

$original_json_string = '[{"id": 3,"lat": "38.8978378","lon": "-77.0365123"},{"id": 4,"lat": "44.8","lon": "1.7"},{"id": 22,"lat": "37.59046","lon": "-122.348994"}]'; 
$original_data = json_decode($original_json_string, true); 
$coordinates = array(); 
foreach($original_data as $key => $value) { 
    $coordinates[] = array('lat' => $value['lat'], 'lon' => $value['lon']); 
} 
$new_data = array(
    'type' => 'FeatureCollection', 
    'features' => array(
     'type' => 'Feature', 
     'geometry' => array('type' => 'Point', 'coordinates' => $coordinates), 
     'properties' => array('name' => 'value'), 
    ), 
); 

$final_data = json_encode($new_data, JSON_PRETTY_PRINT); 

print_r($final_data); 

$final_data 같은 것을 양보한다 : 나는 루프를 수정

{ 
    "type": "FeatureCollection", 
    "features": { 
     "type": "Feature", 
     "geometry": { 
      "type": "Point", 
      "coordinates": [ 
       { 
        "lat": "38.8978378", 
        "lon": "-77.0365123" 
       }, 
       { 
        "lat": "44.8", 
        "lon": "1.7" 
       }, 
       { 
        "lat": "37.59046", 
        "lon": "-122.348994" 
       } 
      ] 
     }, 
     "properties": { 
      "name": "value" 
     } 
    } 
} 
+1

멋진 감사합니다! 정확히 내가 필요로하는 것, 구문은 나와 함께 철자가되어 있고, 나는 냉소적이지 않다. =) – aibarra