2017-08-29 8 views
0

jsonを解析しようとすると小さな問題が発生します。私のループは、5回のマッチと思われるjsonを手動でチェックすることで、2回だけ実行されます。だから私は疑問に思っていることは、私はこの権利を得るために何ができるのだろうか?私が得ることができるあらゆる助けのために事前に感謝します。これは私の出力であるPHPでネストされたjsonを解析すると5つのうち2つしか表示されない

$url = "https://example.com"; 
$result = file_get_contents($url); 
    // Will dump a beauty json :3 
    $decoded = json_decode($result); 
    $comments = $decoded->tournaments[0]->events; 
    foreach($comments as $comment){ 
    $homeTeam = $comment->homeTeam->name; 
    $homeScore = $comment->homeScore->current; 
    $awayTeam = $comment->awayTeam->name; 
    $awayScore = $comment->awayScore->current; 
    $status = $comment->status->type; 
    //do something with it 
    echo $homeTeam; 
    echo "</br>"; 
    echo $homeScore; 
    echo "</br>"; 
    echo $awayTeam; 
    echo "</br>"; 
    echo $awayScore; 
    echo "</br>"; 
    echo $status; 
    echo "</br>"; 
    } 

:ここ

は私のPHPコードである

Skjern Handbold 
24 
GOG Handbold 
23 
finished 
Aalborg Handbold 
32 
Skjern Handbold 
24 
finished 

そして、ここで(申し訳ありませんJSONの例ですが、私は私が共有することは許されないと思いますソース)

{ 
    "sport":{ 
     "name":"Handball", 
     "slug":"handball", 
     "id":6 
    }, 
    "rows":1, 
    "tournaments":[ 
     { 
     "tournament":{ 
      "name":"Handboldligaen Qualification Round, Group 1", 
      "slug":"handboldligaen-qualification-round-group-1", 
      "id":10600, 
      "uniqueId":157, 
      "uniqueName":"Handboldligaen", 
      "hasEventPlayerStatistics":false, 
      "hasEventPlayerHeatMap":false 
     }, 
     "category":{ 
      "name":"Denmark", 
      "slug":"denmark", 
      "priority":0, 
      "id":59, 
      "flag":"denmark" 
     }, 
     "season":{ 
      "name":"Handboldligaen 16\/17", 
      "slug":"handboldligaen-1617", 
      "year":"16\/17", 
      "id":12321 
     }, 
     "hasEventPlayerStatistics":false, 
     "hasEventPlayerHeatMap":false, 
     "events":[ 
      { 
       "sport":{ 
        "name":"Handball", 
        "slug":"handball", 
        "id":6 
       }, 
       "roundInfo":{ 
        "round":5 
       }, 
       "customId":"FGbsNGb", 
       "status":{ 
        "code":100, 
        "type":"finished" 
       }, 
       "winnerCode":1, 
       "homeTeam":{ 
        "name":"Skjern Handbold", 
        "slug":"skjern-handbold", 
        "gender":"M", 
        "id":4080, 
        "subTeams":[ 

        ] 
       }, 
       "awayTeam":{ 
        "name":"GOG Handbold", 
        "slug":"gog-handbold", 
        "gender":"M", 
        "id":4088, 
        "subTeams":[ 

        ] 
       }, 
       "homeScore":{ 
        "current":24, 
        "period1":12, 
        "normaltime":24 
       }, 
       "awayScore":{ 
        "current":23, 
        "period1":13, 
        "normaltime":23 
       }, 
       "time":{ 

       }, 
       "changes":{ 
        "changeDate":"2017-04-26T20:07:28+00:00", 
        "changes":[ 
        "status.code", 
        "status.description", 
        "status.type", 
        "homeScore.normaltime", 
        "awayScore.normaltime" 
        ], 
        "changeTimestamp":1493237248, 
        "hasExpired":true, 
        "hasHomeChanges":false, 
        "hasAwayChanges":false 
       }, 
       "hasHighlights":false, 
       "hasHighlightsStream":false, 
       "id":7380204, 
       "hasStatistics":true, 
       "name":"Skjern Handbold - GOG Handbold", 
       "startTime":"18:30", 
       "formatedStartDate":"26.04.2017.", 
       "startTimestamp":1493231400, 
       "statusDescription":"FT", 
       "slug":"gog-handbold-skjern-handbold", 
       "uniqueTournamentId":157, 
       "hasLineups":false, 
       "hasLineupsList":false, 
       "hasOdds":true, 
       "hasLiveOdds":true, 
       "hasSubScore":false, 
       "hasAggregatedScore":false, 
       "hasFirstToServe":false, 
       "hasDraw":true, 
       "votingEnabled":false, 
       "hasTime":false, 
       "isSyncable":true, 
       "confirmedLineups":false 
      }, 
      { 
       "sport":{ 
        "name":"Handball", 
        "slug":"handball", 
        "id":6 
       }, 
       "roundInfo":{ 
        "round":6 
       }, 
       "customId":"FGbsKGb", 
       "status":{ 
        "code":100, 
        "type":"finished" 
       }, 
       "winnerCode":1, 
       "homeTeam":{ 
        "name":"Aalborg Handbold", 
        "slug":"aalborg-handbold", 
        "gender":"M", 
        "id":4085, 
        "subTeams":[ 

        ] 
       }, 
       "awayTeam":{ 
        "name":"Skjern Handbold", 
        "slug":"skjern-handbold", 
        "gender":"M", 
        "id":4080, 
        "subTeams":[ 

        ] 
       }, 
       "homeScore":{ 
        "current":32, 
        "period1":18, 
        "normaltime":32 
       }, 
       "awayScore":{ 
        "current":24, 
        "period1":13, 
        "normaltime":24 
       }, 
       "time":{ 

       }, 
       "changes":{ 
        "changeDate":"2017-04-30T17:58:53+00:00", 
        "changes":[ 
        "status.code", 
        "status.description", 
        "status.type", 
        "homeScore.normaltime", 
        "awayScore.normaltime" 
        ], 
        "changeTimestamp":1493575133, 
        "hasExpired":true, 
        "hasHomeChanges":false, 
        "hasAwayChanges":false 
       }, 
       "hasHighlights":false, 
       "hasHighlightsStream":false, 
       "id":7380206, 
       "hasStatistics":true, 
       "name":"Aalborg Handbold - Skjern Handbold", 
       "startTime":"16:30", 
       "formatedStartDate":"30.04.2017.", 
       "startTimestamp":1493569800, 
       "statusDescription":"FT", 
       "slug":"aalborg-handbold-skjern-handbold", 
       "uniqueTournamentId":157, 
       "hasLineups":false, 
       "hasLineupsList":false, 
       "hasOdds":true, 
       "hasLiveOdds":false, 
       "hasSubScore":false, 
       "hasAggregatedScore":false, 
       "hasFirstToServe":false, 
       "hasDraw":true, 
       "votingEnabled":false, 
       "hasTime":false, 
       "isSyncable":true, 
       "confirmedLineups":false 
      } 
     ] 
     }, 
     { 
     "tournament":{ 
      "name":"Handboldligaen, Playoffs", 
      "slug":"handboldligaen-playoffs", 
      "id":5918, 
      "uniqueId":157, 
      "uniqueName":"Handboldligaen", 
      "hasEventPlayerStatistics":false, 
      "hasEventPlayerHeatMap":false 
     }, 
     "category":{ 
      "name":"Denmark", 
      "slug":"denmark", 
      "priority":0, 
      "id":59, 
      "flag":"denmark" 
     }, 
     "season":{ 
      "name":"Handboldligaen 16\/17", 
      "slug":"handboldligaen-1617", 
      "year":"16\/17", 
      "id":12321 
     }, 
     "hasEventPlayerStatistics":false, 
     "hasEventPlayerHeatMap":false, 
     "events":[ 
      { 
       "sport":{ 
        "name":"Handball", 
        "slug":"handball", 
        "id":6 
       }, 
       "roundInfo":{ 
        "round":2, 
        "name":"Semifinals" 
       }, 
       "customId":"FGbsnFE", 
       "status":{ 
        "code":100, 
        "type":"finished" 
       }, 
       "winnerCode":1, 
       "homeTeam":{ 
        "name":"Skjern Handbold", 
        "slug":"skjern-handbold", 
        "gender":"M", 
        "id":4080, 
        "subTeams":[ 

        ] 
       }, 
       "awayTeam":{ 
        "name":"Ribe-Esbjerg HH", 
        "slug":"ribe-esbjerg-hh", 
        "gender":"M", 
        "id":74013, 
        "subTeams":[ 

        ] 
       }, 
       "homeScore":{ 
        "current":26, 
        "period1":13, 
        "normaltime":26 
       }, 
       "awayScore":{ 
        "current":24, 
        "period1":12, 
        "normaltime":24 
       }, 
       "time":{ 

       }, 
       "changes":{ 
        "changeDate":"2017-05-11T20:10:09+00:00", 
        "changes":[ 
        "status.code", 
        "status.description", 
        "status.type", 
        "homeScore.normaltime", 
        "awayScore.normaltime" 
        ], 
        "changeTimestamp":1494533409, 
        "hasExpired":true, 
        "hasHomeChanges":false, 
        "hasAwayChanges":false 
       }, 
       "hasHighlights":false, 
       "hasHighlightsStream":false, 
       "id":7406145, 
       "hasStatistics":true, 
       "name":"Skjern Handbold - Ribe-Esbjerg HH", 
       "startTime":"18:30", 
       "formatedStartDate":"11.05.2017.", 
       "startTimestamp":1494527400, 
       "statusDescription":"FT", 
       "slug":"ribe-esbjerg-hh-skjern-handbold", 
       "uniqueTournamentId":157, 
       "hasLineups":false, 
       "hasLineupsList":false, 
       "hasOdds":true, 
       "hasLiveOdds":true, 
       "hasSubScore":false, 
       "hasAggregatedScore":false, 
       "hasFirstToServe":false, 
       "hasDraw":true, 
       "votingEnabled":false, 
       "hasTime":false, 
       "isSyncable":true, 
       "confirmedLineups":false 
      }, 
      { 
       "sport":{ 
        "name":"Handball", 
        "slug":"handball", 
        "id":6 
       }, 
       "roundInfo":{ 
        "round":2, 
        "name":"Semifinals" 
       }, 
       "customId":"FGbsnFE", 
       "status":{ 
        "code":100, 
        "type":"finished" 
       }, 
       "winnerCode":2, 
       "homeTeam":{ 
        "name":"Ribe-Esbjerg HH", 
        "slug":"ribe-esbjerg-hh", 
        "gender":"M", 
        "id":74013, 
        "subTeams":[ 

        ] 
       }, 
       "awayTeam":{ 
        "name":"Skjern Handbold", 
        "slug":"skjern-handbold", 
        "gender":"M", 
        "id":4080, 
        "subTeams":[ 

        ] 
       }, 
       "homeScore":{ 
        "current":26, 
        "period1":12, 
        "normaltime":26 
       }, 
       "awayScore":{ 
        "current":31, 
        "period1":18, 
        "normaltime":31 
       }, 
       "time":{ 

       }, 
       "changes":{ 
        "changeDate":"2017-05-14T15:48:53+00:00", 
        "changes":[ 
        "status.code", 
        "status.description", 
        "status.type", 
        "homeScore.normaltime", 
        "awayScore.normaltime" 
        ], 
        "changeTimestamp":1494776933, 
        "hasExpired":true, 
        "hasHomeChanges":false, 
        "hasAwayChanges":false 
       }, 
       "hasHighlights":false, 
       "hasHighlightsStream":false, 
       "id":7406146, 
       "hasStatistics":true, 
       "name":"Ribe-Esbjerg HH - Skjern Handbold", 
       "startTime":"14:10", 
       "formatedStartDate":"14.05.2017.", 
       "startTimestamp":1494771000, 
       "statusDescription":"FT", 
       "slug":"ribe-esbjerg-hh-skjern-handbold", 
       "uniqueTournamentId":157, 
       "hasLineups":false, 
       "hasLineupsList":false, 
       "hasOdds":true, 
       "hasLiveOdds":true, 
       "hasSubScore":false, 
       "hasAggregatedScore":false, 
       "hasFirstToServe":false, 
       "hasDraw":true, 
       "votingEnabled":false, 
       "hasTime":false, 
       "isSyncable":true, 
       "confirmedLineups":false 
      }, 
      { 
       "sport":{ 
        "name":"Handball", 
        "slug":"handball", 
        "id":6 
       }, 
       "roundInfo":{ 
        "round":1, 
        "name":"Final" 
       }, 
       "customId":"FGbsKGb", 
       "status":{ 
        "code":100, 
        "type":"finished" 
       }, 
       "winnerCode":3, 
       "homeTeam":{ 
        "name":"Aalborg Handbold", 
        "slug":"aalborg-handbold", 
        "gender":"M", 
        "id":4085, 
        "subTeams":[ 

        ] 
       }, 
       "awayTeam":{ 
        "name":"Skjern Handbold", 
        "slug":"skjern-handbold", 
        "gender":"M", 
        "id":4080, 
        "subTeams":[ 

        ] 
       }, 
       "homeScore":{ 
        "current":26, 
        "period1":14, 
        "normaltime":26 
       }, 
       "awayScore":{ 
        "current":26, 
        "period1":11, 
        "normaltime":26 
       }, 
       "time":{ 

       }, 
       "changes":{ 
        "changeDate":"2017-05-25T15:51:05+00:00", 
        "changes":[ 
        "status.code", 
        "status.description", 
        "status.type", 
        "homeScore.normaltime", 
        "awayScore.normaltime" 
        ], 
        "changeTimestamp":1495727465, 
        "hasExpired":true, 
        "hasHomeChanges":false, 
        "hasAwayChanges":false 
       }, 
       "hasHighlights":false, 
       "hasHighlightsStream":false, 
       "id":7416916, 
       "hasStatistics":true, 
       "name":"Aalborg Handbold - Skjern Handbold", 
       "startTime":"14:10", 
       "formatedStartDate":"25.05.2017.", 
       "startTimestamp":1495721400, 
       "statusDescription":"FT", 
       "slug":"aalborg-handbold-skjern-handbold", 
       "uniqueTournamentId":157, 
       "hasLineups":false, 
       "hasLineupsList":false, 
       "hasOdds":true, 
       "hasLiveOdds":true, 
       "hasSubScore":false, 
       "hasAggregatedScore":false, 
       "hasFirstToServe":false, 
       "hasDraw":true, 
       "votingEnabled":false, 
       "hasTime":false, 
       "isSyncable":true, 
       "confirmedLineups":false 
      }, 
      { 
       "sport":{ 
        "name":"Handball", 
        "slug":"handball", 
        "id":6 
       }, 
       "roundInfo":{ 
        "round":1, 
        "name":"Final" 
       }, 
       "customId":"FGbsKGb", 
       "status":{ 
        "code":100, 
        "type":"finished" 
       }, 
       "winnerCode":2, 
       "homeTeam":{ 
        "name":"Skjern Handbold", 
        "slug":"skjern-handbold", 
        "gender":"M", 
        "id":4080, 
        "subTeams":[ 

        ] 
       }, 
       "awayTeam":{ 
        "name":"Aalborg Handbold", 
        "slug":"aalborg-handbold", 
        "gender":"M", 
        "id":4085, 
        "subTeams":[ 

        ] 
       }, 
       "homeScore":{ 
        "current":25, 
        "period1":10, 
        "normaltime":25 
       }, 
       "awayScore":{ 
        "current":32, 
        "period1":14, 
        "normaltime":32 
       }, 
       "time":{ 

       }, 
       "changes":{ 
        "changeDate":"2017-05-28T15:47:33+00:00", 
        "changes":[ 
        "status.code", 
        "status.description", 
        "status.type", 
        "homeScore.normaltime", 
        "awayScore.normaltime" 
        ], 
        "changeTimestamp":1495986453, 
        "hasExpired":true, 
        "hasHomeChanges":false, 
        "hasAwayChanges":false 
       }, 
       "hasHighlights":false, 
       "hasHighlightsStream":false, 
       "id":7416915, 
       "hasStatistics":true, 
       "name":"Skjern Handbold - Aalborg Handbold", 
       "startTime":"14:10", 
       "formatedStartDate":"28.05.2017.", 
       "startTimestamp":1495980600, 
       "statusDescription":"FT", 
       "slug":"aalborg-handbold-skjern-handbold", 
       "uniqueTournamentId":157, 
       "hasLineups":false, 
       "hasLineupsList":false, 
       "hasOdds":true, 
       "hasLiveOdds":true, 
       "hasSubScore":false, 
       "hasAggregatedScore":false, 
       "hasFirstToServe":false, 
       "hasDraw":true, 
       "votingEnabled":false, 
       "hasTime":false, 
       "isSyncable":true, 
       "confirmedLineups":false 
      } 
     ] 
     }, 
     { 
     "tournament":{ 
      "name":"Cup", 
      "slug":"cup", 
      "id":244, 
      "uniqueId":1319, 
      "uniqueName":"Cup", 
      "hasEventPlayerStatistics":false, 
      "hasEventPlayerHeatMap":false 
     }, 
     "category":{ 
      "name":"Denmark", 
      "slug":"denmark", 
      "priority":0, 
      "id":59, 
      "flag":"denmark" 
     }, 
     "season":{ 
      "name":"Cup 17\/18", 
      "slug":"cup-1718", 
      "year":"17\/18", 
      "id":14155 
     }, 
     "hasEventPlayerStatistics":false, 
     "hasEventPlayerHeatMap":false, 
     "events":[ 
      { 
       "sport":{ 
        "name":"Handball", 
        "slug":"handball", 
        "id":6 
       }, 
       "roundInfo":{ 
        "round":4, 
        "name":"1\/8" 
       }, 
       "customId":"FGbsQpM", 
       "status":{ 
        "code":100, 
        "type":"finished" 
       }, 
       "winnerCode":2, 
       "homeTeam":{ 
        "name":"Sydhavsoerne", 
        "slug":"sydhavsoerne", 
        "gender":"M", 
        "id":93291, 
        "subTeams":[ 

        ] 
       }, 
       "awayTeam":{ 
        "name":"Skjern Handbold", 
        "slug":"skjern-handbold", 
        "gender":"M", 
        "id":4080, 
        "subTeams":[ 

        ] 
       }, 
       "homeScore":{ 
        "current":21, 
        "period1":12, 
        "normaltime":21 
       }, 
       "awayScore":{ 
        "current":31, 
        "period1":16, 
        "normaltime":31 
       }, 
       "time":{ 

       }, 
       "changes":{ 
        "changes":[ 

        ], 
        "changeTimestamp":0, 
        "hasExpired":true, 
        "hasHomeChanges":false, 
        "hasAwayChanges":false 
       }, 
       "hasHighlights":false, 
       "hasHighlightsStream":false, 
       "id":7538722, 
       "hasStatistics":false, 
       "name":"Sydhavsoerne - Skjern Handbold", 
       "startTime":"17:30", 
       "formatedStartDate":"23.08.2017.", 
       "startTimestamp":1503509400, 
       "statusDescription":"FT", 
       "slug":"sydhavsoerne-skjern-handbold", 
       "uniqueTournamentId":1319, 
       "hasLineups":false, 
       "hasLineupsList":false, 
       "hasOdds":true, 
       "hasLiveOdds":false, 
       "hasSubScore":false, 
       "hasAggregatedScore":false, 
       "hasFirstToServe":false, 
       "hasDraw":true, 
       "votingEnabled":false, 
       "hasTime":false, 
       "isSyncable":true, 
       "confirmedLineups":false 
      } 
     ] 
     }, 
     { 
     "tournament":{ 
      "name":"Super Cup", 
      "slug":"super-cup", 
      "id":17307, 
      "uniqueId":10493, 
      "uniqueName":"Super Cup", 
      "hasEventPlayerStatistics":false, 
      "hasEventPlayerHeatMap":false 
     }, 
     "category":{ 
      "name":"Denmark", 
      "slug":"denmark", 
      "priority":0, 
      "id":59, 
      "flag":"denmark" 
     }, 
     "season":{ 
      "name":"Super Cup 2017", 
      "slug":"super-cup-2017", 
      "year":"2017", 
      "id":14279 
     }, 
     "hasEventPlayerStatistics":false, 
     "hasEventPlayerHeatMap":false, 
     "events":[ 
      { 
       "sport":{ 
        "name":"Handball", 
        "slug":"handball", 
        "id":6 
       }, 
       "roundInfo":{ 
        "round":1, 
        "name":"Final" 
       }, 
       "customId":"FGbsKGb", 
       "status":{ 
        "code":100, 
        "type":"finished" 
       }, 
       "winnerCode":1, 
       "homeTeam":{ 
        "name":"Skjern Handbold", 
        "slug":"skjern-handbold", 
        "gender":"M", 
        "id":4080, 
        "subTeams":[ 

        ] 
       }, 
       "awayTeam":{ 
        "name":"Aalborg Handbold", 
        "slug":"aalborg-handbold", 
        "gender":"M", 
        "id":4085, 
        "subTeams":[ 

        ] 
       }, 
       "homeScore":{ 
        "current":27, 
        "period1":13, 
        "normaltime":27 
       }, 
       "awayScore":{ 
        "current":17, 
        "period1":10, 
        "normaltime":17 
       }, 
       "time":{ 

       }, 
       "changes":{ 
        "changeDate":"2017-08-29T20:03:46+00:00", 
        "changes":[ 
        "status.code", 
        "status.description", 
        "status.type", 
        "homeScore.normaltime", 
        "awayScore.normaltime", 
        "homeScore.current" 
        ], 
        "changeTimestamp":1504037026, 
        "hasExpired":true, 
        "hasHomeChanges":false, 
        "hasAwayChanges":false 
       }, 
       "hasHighlights":false, 
       "hasHighlightsStream":false, 
       "id":7552114, 
       "hasStatistics":false, 
       "name":"Skjern Handbold - Aalborg Handbold", 
       "startTime":"18:30", 
       "formatedStartDate":"29.08.2017.", 
       "startTimestamp":1504031400, 
       "statusDescription":"FT", 
       "slug":"aalborg-handbold-skjern-handbold", 
       "uniqueTournamentId":10493, 
       "hasLineups":false, 
       "hasLineupsList":false, 
       "hasOdds":true, 
       "hasLiveOdds":true, 

      } 
     ] 
     } 
     ] 
     } 
    ] 
} 

答えて

1

あなたの質問を理解できません。

まず、一例で与えられたJSONは完全には有効ではありません。

余分なコンマが最後

"hasLiveOdds":true, 

といくつかの余分な括弧の後にあります:一度

 ] 
    } 
    ] 
    } 

あなたの考えを理解すれば、5の代わりに8つのマッチが見つかります。

Whate ver、ここに私の提案があります:

$decoded = json_decode($result); 
$tournaments = $decoded->tournaments; 
foreach ($tournaments as $tournament) { 
    $comments = $tournament->events; 
    foreach ($comments as $comment) { 
     // Your code 
    } 
} 
+0

ありがとう!あなたは私を完全に理解した!うん、それは私がより困難に役立っている場合、それは残念にフィットするように、括弧のカップルを殺すためにしなければならなかった。再度、感謝します! –

関連する問題