2017-06-29 18 views
0

これはチケットシステムから取得したJSONデータです。私はそれをPHPで解析し、データベースにデータを保存して統計とダッシュボードを作成して、誰が何枚のチケットを開いているのか、最新のクローズドチケットを確認できるようにしたいと思います。 私はいくつかのデータを読むことができますが、すべてを読むことはできません。PHPでjson_decodeを使用してJSONを解析する

{ 
    "address":"belgium", 
    "workers":{ 
     "peter":{ 
      "worker":"peter", 
      "open_close_time":"45.6 T/h", 
      "closed_tickets":841, 
      "open_tickets":7, 
      "last_checkin":1498768133, 
      "days_too_late":0 
     }, 
     "mark":{ 
      "worker":"mark", 
      "open_close_time":"45.9 T/h", 
      "closed_tickets":764, 
      "open_tickets":2, 
      "last_checkin":1498768189, 
      "days_too_late":0 
     }, 
     "walter":{ 
      "worker":"walter", 
      "open_close_time":"20.0 T/h", 
      "closed_tickets":595, 
      "open_tickets":4, 
      "last_checkin":1498767862, 
      "days_too_late":0 
     } 
    }, 
    "total_tickets":2213, 
    "tickets":[ 
     { 
      "id":2906444760, 
      "client":"297", 
      "processed":0 
     }, 
     { 
      "id":2260, 
      "client":"121", 
      "processed":0 
     }, 
     { 
      "id":2424, 
      "client":"45", 
      "processed":0 
     } 
    ], 
    "last_closed_tickets":[ 
     { 
      "id":2259, 
      "client":"341", 
      "closed_on":"2017-06-25T10:11:00.000Z" 
     }, 
     { 
      "id":2258, 
      "client":"48", 
      "closed_on":"2017-06-20T18:37:03.000Z" 
     } 
    ], 
    "settings":{ 
     "address":"belgium", 
     "email":"", 
     "daily_stats":0 
    }, 
    "open_close_time":"161.1 T/h", 
    "avgopen_close_time":123298, 
    "ticket_time":"27.1 T/h", 
    "stats":{ 
     "time":1498768200087, 
     "newest_ticket":1498768189000, 
     "closed_tickets":2200, 
     "open_tickets":13, 
     "active_workers":3 
    }, 
    "avg_paid_tickets":64.55, 
    "avg_afterservice_tickets":35.45 
} 

これは私がワーカーの名前を取得しようとしたPHPコードですが、これは動作しません。

<?php 
$string = file_get_contents("example.json"); 
$json = json_decode($string, true); 
echo $json['address']; 
foreach($json->workers->new as $entry) { 
    echo $entry->worker; 
} 
?> 

それが動作の下に、私はここのようにそれを試してみたが、その後、私は毎回別の従業員が開始コードを変更する持っていれば。 json_decode``ある `` True``の ``で2番目のパラメータで

echo $json['workers']['mark']['closed_tickets']; 
+3

。 >返されたオブジェクトは、連想配列に変換されます。 '$ foreach($ json ['workers']を$ entryとして使ってみてください)' ' – alistaircol

+0

あなたが作業している構造体を見るために配列をダンプすることが疑わしいならば。 – Doug

答えて

1
<?php 
$string = file_get_contents("example.json"); 
$json = json_decode($string, true); 
foreach($json['workers'] as $entry){ 
     echo $entry['worker'] . " has " . $entry['open_tickets'] . " tickets" . "\n"; 
} 
?> 
+0

ようこそ@ StackOverflow @Tom。あなたの問題を解決した場合は、この回答を正しいものとしてマークしてください。 –

関連する問題