Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

Simple PHP: Extract a PHP variable from an array

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...