PHP:如何使用“怪异”访问对象内部的对象;“关键”;名称
我有一个从远程Web服务返回的JSON对象(通过Curl调用)。对象是这样的:PHP:如何使用“怪异”访问对象内部的对象;“关键”;名称,php,json,object,Php,Json,Object,我有一个从远程Web服务返回的JSON对象(通过Curl调用)。对象是这样的: stdClass Object ( [https://example.com] => stdClass Object ( [hash] => 8 [id] => 277 ) ) 我应该如何从这个对象访问像:hash,id这样的值 我试过: $Object = json_decode( $curl_return ); echo $Object->hash; // Didn't work! e
stdClass Object ( [https://example.com] => stdClass Object ( [hash] => 8 [id] => 277 ) )
我应该如何从这个对象访问像:hash
,id
这样的值
我试过:
$Object = json_decode( $curl_return );
echo $Object->hash; // Didn't work!
echo $Object[0]->hash; // Didn't work!
echo $Object[0]['hash']; // Didn't work!
echo $Object['https://example.com']->hash; // Didn't work!
请帮忙。这将有效:
$url = 'https://example.com';
echo $Object->$url->hash;
或者,通过将第二个参数设置为true
,可以将JSON解码为关联数组,而不是\stdClass
:
json_decode($json, true);
将
TRUE
作为第二个参数传递给,并返回数组,而不是对象
然后,您所要做的就是使用通常的语法(带方括号)访问这些值:
echo($Object['https://example.com']['hash']);
还要确保不要忽略:因为这里的属性名无效。