[英]Foreach through JSON stdClass Object array in PHP
I am fetching a JSON api in PHP, then I am attempting to iterate through the array with a foreach($obj['result'] as $c)
loop.我正在 PHP 中获取 JSON api,然后我尝试使用foreach($obj['result'] as $c)
循环遍历数组。 I'm specifically trying to loop through the [result] nested array, but the returned stdClass Object
is not letting me do it.我专门尝试遍历 [result] 嵌套数组,但返回的stdClass Object
不允许我这样做。 I have attempted to use $obj = get_object_vars($obj)
but that doesn't seem to work for the $obj['result']
lower nested array.我曾尝试使用$obj = get_object_vars($obj)
但这似乎不适用于$obj['result']
下层嵌套数组。 Any ideas what I do here to foreach through the $obj['result']
?我在这里通过$obj['result']
foreach 做些什么有什么想法吗?
$json = file_get_contents($rink);
$obj = array();
$obj = json_decode($json);
When I use php print_r()
I get.当我使用 php print_r()
时,我得到了。
stdClass Object (
[status] => 1
[message] => OK
[result] => Array
(
[0] => stdClass Object
(
[blockNumber] => 10271028
[timeStamp] => 1646413513
[hash] => 0xab6f7d30aec4f...
)
[1] => stdClass Object
(
[blockNumber] => 10271028
[timeStamp] => 1646413513
[hash] => 0xab6f7d30aec4f...
)
)
)
Php foreach attempted loop Php foreach 尝试循环
foreach($obj['result'] as $c) {
echo $c['hash'] . '<br>';
}
Change改变
foreach($obj['result'] as $c) {
echo $c['hash'] . '<br>';
}
to到
foreach($obj->result as $c) {
echo $c->hash . '<br>';
}
Object key value is accessed with ->
operator in php. Object 键值在 php 中使用->
运算符访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.