I am trying to Json decode something and get the value I want. But I get PHP undefined index error. This is my code.
我正在尝试Json解码一些东西并获得我想要的价值。但我得到PHP未定义的索引错误。这是我的代码。
<?php
$json = '[{"totalGamesPlayed":25,"championId":0}]';
$data = json_decode($json,true);
$games = $data['totalGamesPlayed'];
echo $games;
?>
The problem is the "[" "]" are messing with my code... I am using an API to get some values. What I get is this : http://pastebin.com/XrqkAbJf I need the totalGamesPlayed,the champion ID's except zero (82,106 and 24) and the TOTAL_SESSIONS_WON and TOTAL_SESSIONS_LOST for these ID's... For a start, let's find out how can I bypass the "[" and "]" symbols,and then things may be easier.. Thank you in advance!
问题是“[”“]”正在弄乱我的代码......我正在使用API来获取一些值。我得到的是:http://pastebin.com/XrqkAbJf我需要totalGamesPlayed,除了零(82,106和24)之外的冠军ID以及这些ID的TOTAL_SESSIONS_WON和TOTAL_SESSIONS_LOST ......首先,让我们看看怎么样?我绕过“[”和“]”符号,然后事情可能会更容易..提前谢谢你!
3 个解决方案
#1
3
Access your code like this
像这样访问你的代码
$games = $data[0]['totalGamesPlayed'];
Code for getting other info
获取其他信息的代码
<?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);
}
}
#2
1
Have you try this:
你试试这个:
$games = $data[0]['totalGamesPlayed'];
The problem is your json is an array with it first element is an object
问题是你的json是一个数组,第一个元素是一个对象
#3
1
In case of problems like yours, it's handy to peek what your decode data really looks like. So instead of blindly reading, use print_r()
or var_dump()
so look. print_r($data);
would output:
如果您遇到类似问题,可以方便地查看解码数据的真实情况。因此,不要盲目阅读,请使用print_r()或var_dump()。的print_r($数据);输出:
Array
(
[0] => Array
(
[totalGamesPlayed] => 25
[championId] => 0
)
)
therefore the correct "path" is:
因此,正确的“路径”是:
$games = $data[0]['totalGamesPlayed'];
This is so, because your JSON object is array (first and last character of JSON is [
and ]
) with object as array node ({
/}
) and your real values are members of that object. You can fix that by checking why you construct JSON that way in the first place (maybe code allows more elements of array), or "extract" object to get rid of being forced to use [0]
in references:
就是这样,因为您的JSON对象是数组(JSON的第一个和最后一个字符是[和]),对象是数组节点({/}),您的实际值是该对象的成员。你可以通过检查为什么你首先以这种方式构造JSON(也许代码允许更多的数组元素),或者“提取”对象来摆脱被引用中强制使用[0]的方法来解决这个问题:
$data = $data[0];
$games = $data['totalGamesPlayed'];
and print_r($data)
would give:
和print_r($ data)会给出:
Array
(
[totalGamesPlayed] => 25
[championId] => 0
)
and your former code will start to work:
并且您以前的代码将开始工作:
$games = $data['totalGamesPlayed'];
echo $games;
gives
25
#1
3
Access your code like this
像这样访问你的代码
$games = $data[0]['totalGamesPlayed'];
Code for getting other info
获取其他信息的代码
<?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);
}
}
#2
1
Have you try this:
你试试这个:
$games = $data[0]['totalGamesPlayed'];
The problem is your json is an array with it first element is an object
问题是你的json是一个数组,第一个元素是一个对象
#3
1
In case of problems like yours, it's handy to peek what your decode data really looks like. So instead of blindly reading, use print_r()
or var_dump()
so look. print_r($data);
would output:
如果您遇到类似问题,可以方便地查看解码数据的真实情况。因此,不要盲目阅读,请使用print_r()或var_dump()。的print_r($数据);输出:
Array
(
[0] => Array
(
[totalGamesPlayed] => 25
[championId] => 0
)
)
therefore the correct "path" is:
因此,正确的“路径”是:
$games = $data[0]['totalGamesPlayed'];
This is so, because your JSON object is array (first and last character of JSON is [
and ]
) with object as array node ({
/}
) and your real values are members of that object. You can fix that by checking why you construct JSON that way in the first place (maybe code allows more elements of array), or "extract" object to get rid of being forced to use [0]
in references:
就是这样,因为您的JSON对象是数组(JSON的第一个和最后一个字符是[和]),对象是数组节点({/}),您的实际值是该对象的成员。你可以通过检查为什么你首先以这种方式构造JSON(也许代码允许更多的数组元素),或者“提取”对象来摆脱被引用中强制使用[0]的方法来解决这个问题:
$data = $data[0];
$games = $data['totalGamesPlayed'];
and print_r($data)
would give:
和print_r($ data)会给出:
Array
(
[totalGamesPlayed] => 25
[championId] => 0
)
and your former code will start to work:
并且您以前的代码将开始工作:
$games = $data['totalGamesPlayed'];
echo $games;
gives
25