2016-03-30 15 views
0

を抽出するIは緯度/経度の座標を抽出するPHP解析JSON緯度/経度

[{ 
    "options": { 
     "allowUTurn": false 
    }, 
    "latLng": { 
     "lat": 44.91138, 
     "lng": 7.671783 
    }, 
    "name": "Corso Vinovo, Carignano, TO, Piemont, Italy", 
    "_initHooksCalled": true 
}, { 
    "options": { 
     "allowUTurn": false 
    }, 
    "latLng": { 
     "lat": 44.909979, 
     "lng": 7.676234 
    }, 
    "name": "Il Tempio del Pane, Corso Cesare Battisti, Carignano, TO, Piemont, Italy", 
    "_initHooksCalled": true 
}, { 
    "options": { 
     "allowUTurn": false 
    }, 
    "latLng": { 
     "lat": 44.907805, 
     "lng": 7.674952 
    }, 
    "name": "Banca Intesa, Via Ferdinando Salotto, Carignano, TO, Piemont, Italy", 
    "_initHooksCalled": true 
}] 

を使用して、このJSONを解析する必要があります。私は

echo $wayPoints->????? 

とどのように私はすべての座標を抽出するcicleのために作成した後に使用する必要がどのような

ありがとうございました!ありがとうございます!

チェーザレ

EDIT:コードサンプル(JSONをPOSTパラメータから来ることに注意してください...)

<?php 
    echo "Waypoints ...</br>"; 
    echo "</br>"; 
    echo $_POST['wayPoints']; 
    $wayPoints = $_POST['wayPoints']; 

    $json = json_decode($wayPoints); 

    foreach($json as $f){ 
    echo $f['latLng']['lat']; 
    echo $f['latLng']['lng']; 
    } 
?> 

ので、コードが動作しない(...もっと明確にする必要があります。.. )

ありがとうございました...

EDIT 2:このコードの作業!!!

<?php 
    echo "Waypoints ...</br>"; 
    echo "</br>"; 
    echo $_POST['wayPoints']; 
    $wayPoints = $_POST['wayPoints']; 

    $json = json_decode($wayPoints, true); 

    foreach($json as $f){ 
    echo "</br>"; 
    echo $f['latLng']['lat']; 
    echo "</br>"; 
    echo $f['latLng']['lng']; 
    echo "</br>";  } 
?> 

出力は

44.91138 
7.671783 

44.909979 
7.676234 

44.907805 
7.674952 

であるあなたのすべてをありがとう!

foreach($file as $f){ 
echo $f['latLng']['lat']; 
echo $f['latLng']['lng']; 
} 

答えて

1

このあなたのJSONオブジェクトは、foreachので反復その後、連想配列に変換します:

1

まずは$file = json_decode() を行うには、その後、あなただけのforeachに追加しますファイルが必要です。

//use json_decode in associative mode 
$decoded = json_decode($json, true); 

//Your object is now an array called $decoded 
//Your locations are subarrays of $decoded 
//The co-ords are subarrays of each $locationArray 
foreach($decoded as $locationArray) 
{ 
    echo "The co-ordinates for {$locationArray['name']} are: {$locationArray['latLng']['lat']},{$locationArray['latLng']['lng']}" . PHP_EOL; 
} 
+2

あなたは 'json_decode' –

+0

おかげでマルコのための第二パラメータとして' true'をを渡す場合は、この答えはのみ動作することに注意してください...私は私の元の要求に小さなコードサンプルを入れてみました....私はあなたの提案を試みましたが、それは動作していません:-) – Cesare

+0

ブラッド、私は上記の小さなコードサンプルを置くようにしています....今働いていない.. – Cesare