Xaz*_*hki 5 php indexing json decode undefined
我正在尝试Json解码一些东西并获得我想要的价值.但我得到PHP未定义的索引错误.这是我的代码.
<?php
$json = '[{"totalGamesPlayed":25,"championId":0}]';
$data = json_decode($json,true);
$games = $data['totalGamesPlayed'];
echo $games;
?>
Run Code Online (Sandbox Code Playgroud)
问题是"[""]"正在弄乱我的代码......我正在使用API来获取一些值.我得到的是:http: //pastebin.com/XrqkAbJf我需要totalGamesPlayed,除了零(82,106和24)之外的冠军ID以及这些ID的TOTAL_SESSIONS_WON和TOTAL_SESSIONS_LOST ...首先,让我们看看怎么样?我绕过"["和"]"符号,然后事情可能会更容易..提前谢谢!
像这样访问您的代码
$games = $data[0]['totalGamesPlayed'];
Run Code Online (Sandbox Code Playgroud)
获取其他信息的代码
<?php
$json = 'PUT YOUR EXAMPLE JSON HERE';
$data = json_decode($json,true);
$seasonWon = 0;
$seasonPlayed = 0;
foreach($data as $stats) {
if($stats['championId'] != 0) {
echo '<br><br>Total Games Played:'. $stats['totalGamesPlayed'];
echo '<br>champion Ids :'.$stats['championId'];
foreach($stats['stats'] as $stat) {
if($stat['statType'] == 'TOTAL_SESSIONS_WON') {
$seasonWon = $stat['value'];
echo '<br>TOTAL_SESSIONS_WON :'.$seasonWon;
}
if($stat['statType'] == 'TOTAL_SESSIONS_LOST')
echo '<br>TOTAL_SESSIONS_LOST :'.$stat['value'];
if($stat['statType'] == 'TOTAL_SESSIONS_PLAYED') {
$seasonPlayed = $stat['value'];
echo '<br>TOTAL_SESSIONS_PLAYED :'.$seasonPlayed;
}
}
echo '<br>Games Ratio(TOTAL_SESSIONS_WON / TOTAL_SESSIONS_PLAYED): ('. $seasonWon.'/'.$seasonPlayed.'):'. ($seasonWon/$seasonPlayed);
}
}
Run Code Online (Sandbox Code Playgroud)