Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/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
PHP/JSON-stdClass对象_Php_Arrays_Json_Iteration - Fatal编程技术网

PHP/JSON-stdClass对象

PHP/JSON-stdClass对象,php,arrays,json,iteration,Php,Arrays,Json,Iteration,我还是个新手。我需要一些帮助-我有一些JSON,我已经通过一些PHP运行了它,基本上对JSON进行解析和解码,如下所示: stdClass Object ( [2010091907] => stdClass Object ( [home] => stdClass Object ( [score] => stdClass Object (

我还是个新手。我需要一些帮助-我有一些JSON,我已经通过一些PHP运行了它,基本上对JSON进行解析和解码,如下所示:

stdClass Object
(
    [2010091907] => stdClass Object
        (
        [home] => stdClass Object
            (
                [score] => stdClass Object
                    (
                        [1] => 7
                        [2] => 17
                        [3] => 10
                        [4] => 7
                        [5] => 0
                        [T] => 41
                    )

                [abbr] => ATL
                [to] => 2
            )

这实际上一直在继续,但是我的问题是
stdClass对象
部分。我需要能够在for循环中调用它,然后遍历每个部分(home、score、abbr、to等)。我该怎么办

您可以使用
get\u object\u vars()
获取对象的属性数组,或者使用
json\u decode($string,true)调用
json\u decode()
以获取关联数组


示例:

<?php
$foo = array('123456' =>
 array('bar' =>
        array('foo'=>1,'bar'=>2)));


//as object
var_dump($opt1 = json_decode(json_encode($foo)));

echo $opt1->{'123456'}->bar->foo;

foreach(get_object_vars($opt1->{'123456'}->bar) as $key => $value){
    echo $key.':'.$value.PHP_EOL;
}

//as array
var_dump($opt2 = json_decode(json_encode($foo),true));

echo $opt2['123456']['bar']['foo'];

foreach($opt2['123456']['bar'] as $key => $value){
    echo $key.':'.$value.PHP_EOL;
}
?>
object(stdClass)#1 (1) {
  ["123456"]=>
  object(stdClass)#2 (1) {
    ["bar"]=>
    object(stdClass)#3 (2) {
      ["foo"]=>
      int(1)
      ["bar"]=>
      int(2)
    }
  }
}
1
foo:1
bar:2

array(1) {
  [123456]=>
  array(1) {
    ["bar"]=>
    array(2) {
      ["foo"]=>
      int(1)
      ["bar"]=>
      int(2)
    }
  }
}
1
foo:1
bar:2

您可以使用
foreach

stdClass
上迭代
json\u解码($string,true),但是,我遇到了称为数组引用的编号的问题。例如,数字是2010091907,但当我发出
foreach($json->2010091907作为$game)
时,我得到了错误
解析错误:语法错误,意外的T\u LNUMBER,预期的T\u字符串或T\u变量
使用
$json->{'2010091907}
表示“非法”变量名。几乎存在:
foreach($json->{'2010091907}作为$game)
,我得到错误:
警告:为foreach()提供的参数无效。
。请参考上面的JSON to数组输出。我需要能够调用数据,而这些数据似乎是递归数组(不确定正式名称)路线?简单得多,然后继续将它们转换为数组…我不确定我是否遵循了-我通过URL接收JSON。然后我将整个JSON输出放入一个数组中(通过
JSON\u decode($data,true)
。从那里,我想迭代数组内容并调用数组中的某些数据。你能提供一些代码吗,因为我可能做得不对。该死,你是对的。为什么PHP人员没有体面地将
stdClass instanceof Traversable
设置为真我想知道。。。。