简单PHP:从数组中提取PHP变量 [英] Simple PHP: Extract a PHP variable from an array
本文介绍了简单PHP:从数组中提取PHP变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将"shortUrl"元素从该数组中移到一个变量中,但是不能!
I need to get the "shortUrl" element out of this array into a variable but can't !
object(stdClass)#1 (4) {
["errorCode"]=> int(0)
["errorMessage"]=> string(0) ""
["results"]=> object(stdClass)#2 (1) {
["http://www.domain.com"]=> object(stdClass)#3 (5) {
["userHash"]=> string(6) "oSEMki"
["shortKeywordUrl"]=> string(0) ""
["hash"]=> string(6) "oms2ZB"
["shortCNAMEUrl"]=> string(20) "http://bit.ly/LALALA"
["shortUrl"]=> string(20) "http://bit.ly/LALALA"
}
}
["statusCode"]=> string(2) "OK"
}
帮助表示赞赏.
推荐答案
它不是数组,而是一个对象(树).
Its not an array, its an object(-tree).
echo $obj->results->{"http://www.domain.com"}->shortUrl;
应该工作.
我还想您似乎正在接收此结构的JSON吗?然后,您可以使用 json_decode()
的第二个参数来创建一个关联数组它.
Also it looks like you are receiving this structure as JSON I guess? Then you may use the second parameter of json_decode()
to make an associative array out of it.
$array = json_decode($json, true);
echo $array['results']['http://www.domain.com']['shortUrl'];
这篇关于简单PHP:从数组中提取PHP变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文