简单PHP:从数组中提取PHP变量

Jon*_*oss -1 php arrays variables

我需要将此数组中的"shortUrl"元素转换为变量但不能!

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" 
} 
Run Code Online (Sandbox Code Playgroud)

帮助赞赏.

Kin*_*nch 5

它不是一个数组,它是一个对象(-tree).

echo $obj->results->{"http://www.domain.com"}->shortUrl;
Run Code Online (Sandbox Code Playgroud)

应该管用.

另外看起来你接收这个结构就像JSON我猜?然后你可以使用第二个参数json_decode()来制作一个关联数组.

$array = json_decode($json, true);
echo $array['results']['http://www.domain.com']['shortUrl'];
Run Code Online (Sandbox Code Playgroud)