Php 处理stdClass对象

Php 处理stdClass对象,php,json,Php,Json,在谷歌搜索了一段时间后,我发现我可以使用以下代码进行谷歌搜索: $text=$_GET['text']; $results=file_get_contents("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=" . $text); // Too lazy to use cURL $results=json_decode($results); 问题是,$results实际上是一个对象(stdClass),像处理数组

在谷歌搜索了一段时间后,我发现我可以使用以下代码进行谷歌搜索:

$text=$_GET['text'];
$results=file_get_contents("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=" . $text);
// Too lazy to use cURL
$results=json_decode($results);
问题是,
$results
实际上是一个
对象(stdClass)
,像处理数组一样处理它是不起作用的(即,
$results[“responseData”]
)。这是完整的
var\u转储文件

object(stdClass)#1 (3) {
  ["responseData"]=>
  object(stdClass)#2 (2) {
    ["results"]=>
    array(4) {
      [0]=>
      object(stdClass)#3 (8) {
        ["GsearchResultClass"]=>
        string(10) "GwebSearch"
        ["unescapedUrl"]=>
        string(51) "http://www.urbandictionary.com/define.php?term=lulz"
        ["url"]=>
        string(55) "http://www.urbandictionary.com/define.php%3Fterm%3Dlulz"
        ["visibleUrl"]=>
        string(23) "www.urbandictionary.com"
        ["cacheUrl"]=>
        string(73) "http://www.google.com/search?q=cache:9eN_paaTATEJ:www.urbandictionary.com"
        ["title"]=>
        string(29) "Urban Dictionary: <b>lulz</b>"
        ["titleNoFormatting"]=>
        string(22) "Urban Dictionary: lulz"
        ["content"]=>
        string(159) "Beginning as a plural variant of lol, <b>Lulz</b> was originally an exclamation but is now   often used as a noun meaning interesting or funny internet cont..."
      }
      [1]=>
      object(stdClass)#4 (8) {
        ["GsearchResultClass"]=>
        string(10) "GwebSearch"
        ["unescapedUrl"]=>
        string(34) "http://en.wiktionary.org/wiki/lulz"
        ["url"]=>
        string(34) "http://en.wiktionary.org/wiki/lulz"
        ["visibleUrl"]=>
        string(17) "en.wiktionary.org"
        ["cacheUrl"]=>
        string(67) "http://www.google.com/search?q=cache:ksdO4S_AefUJ:en.wiktionary.org"
        ["title"]=>
        string(24) "<b>lulz</b> - Wiktionary"
        ["titleNoFormatting"]=>
        string(17) "lulz - Wiktionary"
        ["content"]=>
        string(173) "Like “Hakuna matata†in The Lion King, “<b>lulz</b>†is not just a word, but a philosophy.   [...] Anonymous has made campaigning sexy for the first time since 1968."
      }
      [2]=>
      object(stdClass)#5 (8) {
        ["GsearchResultClass"]=>
        string(10) "GwebSearch"
        ["unescapedUrl"]=>
        string(51) "http://knowyourmeme.com/memes/i-did-it-for-the-lulz"
        ["url"]=>
        string(51) "http://knowyourmeme.com/memes/i-did-it-for-the-lulz"
        ["visibleUrl"]=>
        string(16) "knowyourmeme.com"
        ["cacheUrl"]=>
        string(66) "http://www.google.com/search?q=cache:ITVRlkQkfckJ:knowyourmeme.com"
        ["title"]=>
        string(45) "I Did It for the <b>Lulz</b> | Know Your Meme"
        ["titleNoFormatting"]=>
        string(38) "I Did It for the Lulz | Know Your Meme"
        ["content"]=>
        string(173) "About I Did it for the <b>Lulz</b> (also known as 4 the <b>lulz</b>) is a popular catchphrase used   to express that one carried out a specific action for the sake of perso."
      }
      [3]=>
      object(stdClass)#6 (8) {
        ["GsearchResultClass"]=>
        string(10) "GwebSearch"
        ["unescapedUrl"]=>
        string(56) "http://www.internetslang.com/LULZ-meaning-definition.asp"
        ["url"]=>
        string(56) "http://www.internetslang.com/LULZ-meaning-definition.asp"
        ["visibleUrl"]=>
        string(21) "www.internetslang.com"
        ["cacheUrl"]=>
        string(71) "http://www.google.com/search?q=cache:UNXME3GmS-AJ:www.internetslang.com"
        ["title"]=>
        string(88) "What does <b>LULZ</b> mean? - <b>LULZ</b> Definition - Meaning of <b>LULZ</b> <b>...</b>"
        ["titleNoFormatting"]=>
        string(60) "What does LULZ mean? - LULZ Definition - Meaning of LULZ ..."
        ["content"]=>
        string(185) "This Internet Slang page is designed to explain what the meaning of <b>LULZ</b> is. The   slang word / acronym / abbreviation <b>LULZ</b> means... . Internet Slang. A list of <b>...</b>"
      }
    }
    ["cursor"]=>
    object(stdClass)#7 (6) {
      ["resultCount"]=>
      string(9) "1,080,000"
      ["pages"]=>
      array(8) {
        [0]=>
        object(stdClass)#8 (2) {
          ["start"]=>
          string(1) "0"
          ["label"]=>
          int(1)
        }
        [1]=>
        object(stdClass)#9 (2) {
          ["start"]=>
          string(1) "4"
          ["label"]=>
          int(2)
        }
        [2]=>
        object(stdClass)#10 (2) {
          ["start"]=>
          string(1) "8"
          ["label"]=>
          int(3)
        }
        [3]=>
        object(stdClass)#11 (2) {
          ["start"]=>
          string(2) "12"
          ["label"]=>
          int(4)
        }
        [4]=>
        object(stdClass)#12 (2) {
          ["start"]=>
          string(2) "16"
          ["label"]=>
          int(5)
        }
        [5]=>
        object(stdClass)#13 (2) {
          ["start"]=>
          string(2) "20"
          ["label"]=>
          int(6)
        }
        [6]=>
        object(stdClass)#14 (2) {
          ["start"]=>
          string(2) "24"
          ["label"]=>
          int(7)
        }
        [7]=>
        object(stdClass)#15 (2) {
          ["start"]=>
          string(2) "28"
          ["label"]=>
          int(8)
        }
      }
      ["estimatedResultCount"]=>
      string(7) "1080000"
      ["currentPageIndex"]=>
      int(0)
      ["moreResultsUrl"]=>
      string(76) "http://www.google.com/search?oe=utf8&ie=utf8&source=uds&start=0&hl=en&q=lulz"
      ["searchResultTime"]=>
      string(4) "0.23"
    }
  }
  ["responseDetails"]=>
  NULL
  ["responseStatus"]=>
  int(200)
}
对象(stdClass)#1(3){
[“响应数据”]=>
对象(标准类)#2(2){
[“结果”]=>
阵列(4){
[0]=>
对象(标准类)#3(8){
[“GsearchResultClass”]=>
字符串(10)“GwebSearch”
[“unescapedUrl”]=>
字符串(51)”http://www.urbandictionary.com/define.php?term=lulz"
[“url”]=>
字符串(55)”http://www.urbandictionary.com/define.php%3Fterm%3Dlulz"
[“visibleUrl”]=>
字符串(23)“www.urbandictionary.com”
[“缓存URL”]=>
字符串(73)”http://www.google.com/search?q=cache:9eN_paaTATEJ:www.urbandictionary.com"
[“标题”]=>
字符串(29)“城市词典:lulz”
[“标题信息”]=>
字符串(22)“城市词典:lulz”
[“内容”]=>
string(159)“作为lol的复数变体开始,Lulz最初是一个感叹词,但现在常用作名词,表示有趣或有趣的互联网内容。”
}
[1]=>
对象(标准类)#4(8){
[“GsearchResultClass”]=>
字符串(10)“GwebSearch”
[“unescapedUrl”]=>
字符串(34)”http://en.wiktionary.org/wiki/lulz"
[“url”]=>
字符串(34)”http://en.wiktionary.org/wiki/lulz"
[“visibleUrl”]=>
字符串(17)“en.wiktionary.org”
[“缓存URL”]=>
字符串(67)”http://www.google.com/search?q=cache:ksdO4S_AefUJ:en.wiktionary.org"
[“标题”]=>
字符串(24)“lulz-Wiktionary”
[“标题信息”]=>
字符串(17)“lulz-Wiktionary”
[“内容”]=>
string(173)“就像《狮子王》中的“Hakuna matata”一样,“lulz”不仅仅是一个词,而是一种哲学。[……]匿名组织自1968年以来首次使竞选活动性感起来。”
}
[2]=>
对象(标准类)#5(8){
[“GsearchResultClass”]=>
字符串(10)“GwebSearch”
[“unescapedUrl”]=>
字符串(51)”http://knowyourmeme.com/memes/i-did-it-for-the-lulz"
[“url”]=>
字符串(51)”http://knowyourmeme.com/memes/i-did-it-for-the-lulz"
[“visibleUrl”]=>
字符串(16)“knowyourmeme.com”
[“缓存URL”]=>
字符串(66)”http://www.google.com/search?q=cache:ITVRlkQkfckJ:knowyourmeme.com"
[“标题”]=>
弦乐(45)“我这样做是为了平静|了解你的模因”
[“标题信息”]=>
弦乐(38)“我这样做是为了平静|了解你的模因”
[“内容”]=>
string(173)“About I do it for the Lulz(也称为4 the Lulz)”是一个流行的流行语,用来表达一个人为了个人的利益而采取的特定行动
}
[3]=>
对象(标准类)#6(8){
[“GsearchResultClass”]=>
字符串(10)“GwebSearch”
[“unescapedUrl”]=>
字符串(56)”http://www.internetslang.com/LULZ-meaning-definition.asp"
[“url”]=>
字符串(56)”http://www.internetslang.com/LULZ-meaning-definition.asp"
[“visibleUrl”]=>
字符串(21)“www.internetslang.com”
[“缓存URL”]=>
字符串(71)”http://www.google.com/search?q=cache:UNXME3GmS-AJ:www.internetslang.com“
[“标题”]=>
字符串(88)“LULZ是什么意思?-LULZ定义-LULZ的含义…”
[“标题信息”]=>
字符串(60)“LULZ是什么意思?-LULZ定义-LULZ的含义…”
[“内容”]=>
string(185)“这个互联网俚语页面旨在解释LULZ的含义。俚语单词/首字母缩略词/缩写LULZ的意思是…互联网俚语。一个
}
}
[“光标”]=>
对象(标准类)#7(6){
[“结果帐户”]=>
字符串(9)“1080000”
[“页面”]=>
阵列(8){
[0]=>
对象(标准类)#8(2){
[“开始”]=>
字符串(1)“0”
[“标签”]=>
int(1)
}
[1]=>
对象(标准类)#9(2){
[“开始”]=>
字符串(1)“4”
[“标签”]=>
int(2)
}
[2]=>
对象(标准类)#10(2){
[“开始”]=>
字符串(1)“8”
[“标签”]=>
int(3)
}
[3]=>
对象(标准类)#11(2){
[“开始”]=>
字符串(2)“12”
[“标签”]=>
int(4)
}
[4]=>
对象(标准类)#12(2){
[“开始”]=>
字符串(2)“16”
[“标签”]=>
内部(5)
}
[5]=>
对象(标准类)#13(2){
[“开始”]=>
字符串(2)“20”
[“标签”]=>
国际(6)
}
[6]=>
对象(标准类)#14(2){
[“开始”]=>
字符串(2)“24”
[“标签”]=>
内部(7)
}
[7]=>
对象(标准类)#15(2){
[“开始”]=>
字符串(2)“28”
[“标签”]=>
内部(8)
}
}
[“estimatedResultCount”]=>
字符串(7)“1080000”
[“currentPageIndex”]=>
int(0)
[“moreResultsUrl”]=>
字符串(76)”http://www.google.com/search?oe=utf8&ie=utf8&source=uds&start=0&hl=en&q=lulz"
[“searchResultTime”]=>
字符串(4)“0.23”
}
}
[“responseDetails”]=>
无效的
[“响应状态”]=>
国际(200)
}

那么,关于如何读取结果数组有什么线索吗?(除了
$results[“responseData”]
,我什么都没试过,因为我以前从未使用过JSON。)

使用箭头操作符访问对象的数据:

$results->responseData;
或者,如果您喜欢数组,可以使用json_decode的第二个参数以数组形式返回json数据,例如:

$results = json_decode($results, true);    
通常,您还可以使用以下命令将任何对象强制转换为数组:

$results = (array) $results;
使用上述任一项,您可以使用p访问您的数据
$results['responseData'];