Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Json解码时PHP未定义索引_Php_Json_Indexing_Undefined_Decode - Fatal编程技术网

从Json解码时PHP未定义索引

从Json解码时PHP未定义索引,php,json,indexing,undefined,decode,Php,Json,Indexing,Undefined,Decode,我正在尝试Json解码一些东西,并获得我想要的值。 但我得到PHP未定义的索引错误。 这是我的密码 <?php $json = '[{"totalGamesPlayed":25,"championId":0}]'; $data = json_decode($json,true); $games = $data['totalGamesPlayed']; echo $games; ?> 问题是“[”“]”正在干扰我的代码。。。 我正在使用API获取一些值。 我得到的是: 我需要

我正在尝试Json解码一些东西,并获得我想要的值。 但我得到PHP未定义的索引错误。 这是我的密码

<?php
$json = '[{"totalGamesPlayed":25,"championId":0}]';
$data = json_decode($json,true); 

$games = $data['totalGamesPlayed'];
echo $games;
?>

问题是“[”“]”正在干扰我的代码。。。 我正在使用API获取一些值。 我得到的是: 我需要全场显示,冠军ID除了零(82106和24) 这些ID的总会话数和总会话数。。。 首先,让我们看看如何绕过“[”和“]”符号,然后事情可能会更简单。。
提前谢谢你

像这样访问您的代码

$games = $data[0]['totalGamesPlayed'];
获取其他信息的代码

<?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);
    }
}
您尝试过以下方法吗:

$games = $data[0]['totalGamesPlayed'];

问题是你的json是一个数组,它的第一个元素是一个对象

如果出现像你这样的问题,你可以很方便地了解解码数据的真实情况。因此,与其盲目阅读,不如使用
print\u r()
var\u dump()
查看<代码>打印(数据)将输出:

Array
(
    [0] => Array
        (
            [totalGamesPlayed] => 25
            [championId] => 0
        )
)
因此,正确的“路径”是:

这是因为您的JSON对象是数组(JSON的第一个和最后一个字符是
[
]
),对象是数组节点(
{
/
}
),并且您的实际值是该对象的成员。您可以通过检查为什么首先以这种方式构造JSON(可能代码允许更多的数组元素)或“提取”对象来避免在引用中被迫使用
[0]
,来解决这个问题:

$data = $data[0];
$games = $data['totalGamesPlayed'];
打印($data)
将给出:

Array
(
    [totalGamesPlayed] => 25
    [championId] => 0
)
您以前的代码将开始工作:

$games = $data['totalGamesPlayed'];
echo $games;
给予


如有疑问,
打印($data)
。非常感谢!你知道我如何获得ID和他们赢得的课程吗/我为此奋斗了4个多小时,而你却在10分钟内做到了……祝福你!:P非常感谢!我不明白什么是chmpid^.=^$stat['championId']^.','^;^-^这些东西是为。。。什么是“?”?什么是“,”和冠军ID不显示。。。我得到“champion ID:”然后为空,而它应该是“champion ID:82106,24”,对吗?在您的示例中,没有具有非零值的champion ID
$games = $data['totalGamesPlayed'];
echo $games;
25