2016-12-20 4 views
0

次のJSONオブジェクトを抽出する方法はわかりません。私はforeachを使用しようとしますが、動作しません。私たちを手伝ってくれますか? ここでJSONファイル:実際にはPHPを使用したJSON複合ツリー

{ 
    "success":true, 
    "moreDataAvailable":true, 
    "ewons": 
     [ 
      { 
       "id":252459, 
       "name": 
       "eWON_TEST", 
       "tags": 
        [ 

         { 
          "id":107060, 
          "name":"TEMP_EXT", 
          "dataType":"Int", 
          "description":"Température extérieure", 
          "alarmHint":"", 
          "value":15.0, 
          "quality":"good", 
          "ewonTagId":1, 
          "history": 
           [ 
            { 
            "date":"2016-05-09T16:06:49Z", 
            "quality":"initialGood", 
            "value":64.0 
            }, 

            { 
            "date":"2016-05-09T16:16:49Z", 
            "quality":"initialGood", 
            "value":6.0 
            }, 

            { 
            "date":"2016-05-09T16:21:49Z", 
            "quality":"initialGood", 
            "value":6.0 
            } 
           ] 
         }, 

         { 
          "id":107072, 
          "name":"TEMP_IN", 
          "dataType":"Int", 
          "description":"Température intérieure", 
          "alarmHint":"", 
          "value":22.0, 
          "quality":"good", 
          "ewonTagId":5, 
          "history": 
           [ 
            { 
            "date":"2016-05-09T17:01:49Z", 
            "quality":"initialGood", 
            "value":22.0 
            }, 

            { 
            "date":"2016-05-09T17:06:49Z", 
            "value":22.0 
            }, 

            { 
            "date":"2016-05-09T17:11:49Z", 
            "value":22.0 
            } 
           ] 

         } 
        ], 
       "lastSynchroDate":"2016-12-16T15:21:22Z" 
      } 
     ] 
} 

、私は、各行に次の情報を各タグの履歴を表示したいと思います:

['id'],['name'],['dataType'],['descritption'],['ewonTagId'],['id(from ewons)'],['name(from ewons)'],['date'],['quality(from history)'],['value'] 

私はこの時点で、次のコードを持っていますが、ループのために試したものは何もありませんでした。

<?php 
//Read JSON file 
$filename ='fichier.json'; 
$json = file_get_contents($filename); 

//convert json object to php associative array 
$data = json_decode($json,true); 

// Loop trough the array 
foreach(???) { 
echo ??? 
} 
?> 
+0

私は今朝試してみましたが、私は成功しませんでした:-( –

+0

この質問に100回も重複があります。 – RiggsFolly

答えて

0

3つのforeachループが必要です。

//convert json object to php associative array 
$data = json_decode($json,true); 

// Loop trough the array 
foreach($data['ewons'] as $_ewon) { 
    foreach ($_eown['tags'] as $_tag) { 
     foreach ($_tag['history'] as $_history) { 
      // now echo your data from $_ewon, $_tag and_history 
      // do a var_dump or print_r if you're unsure about what's exactly in these variables 
     } 
    } 
} 
関連する問題