Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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" 
} 

Help appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
216 views
Welcome To Ask or Share your Answers For Others

1 Answer

Its not an array, its an object(-tree).

echo $obj->results->{"http://www.domain.com"}->shortUrl;

Should work.

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'];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...