2012-12-02 5 views
4

개체를 처리, 나는 구글이 코드를 사용하여 검색 할 수 있습니다 발견 (예 : $results["responseData"]). 이는 전체 var_dump입니다 : 그래서는 stdClass 잠시 동안 인터넷 검색 후

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

, 결과 배열을 읽는 방법에 대한 단서? (내가 전에 JSON을 사용한 적이 같이 $results["responseData"]하지만 새로운 것을 시도하지 않았다.)

+0

잠깐만 객체된다. – Charles

+3

@GiulioMuscarello 그리고 json_decode에 두 번째 인수를 전달할 수 있다는 것을 알고 계셨습니까? –

+1

Giulio - 정답에 체크 표시가되어 있습니까? –

답변

7

는 개체의 데이터에 액세스하려면 화살표 연산자를 사용

$results->responseData; 

또는, 당신은 배열을 선호하는 경우, 당신은 사용할 수 있습니다

$results = (array) $results; 
: 당신은이를 사용하여 배열에 모든 개체를 캐스팅 할 수 일반적으로

$results = json_decode($results, true);  

: json_decode의 2 PARAM 배열, 예를 들어 같은 JSON 데이터를 반환 위의 중 하나를 사용 는

, 당신은 PHP 배열 키 구문을 사용하여 데이터를 액세스 할 수 있습니다

$results['responseData']; 
+1

아니면 그냥'json_decode'의 두 번째 매개 변수를 사용 하시겠습니까? – deceze

+0

@deceze 아, 그만해! 그것은 물론 쉬운 솔루션입니다! 그 대답을 내 대답에 추가했습니다. 다른 stdClass 객체에서도 작동하므로 다른 코드는 그대로 두십시오. :) –

+0

쉬운 방법을 보여주는 +1 ('object-> property'). 또한 새 편집 이후 더 좋습니다. –

2

당신이 $results->responseData를 사용해야합니다 responseData에 도달하기 위해, $results이 객체가 있기 때문이다. 당신이 객체를 가지고 있다면 그것의 속성에 ->으로 접근 할 수 있습니다.

P. 당신은 당신이 등등 $results->responseData->results를 사용해야합니다 results 액세스해야하는 경우 responseData는 또한 [`json_decode`] (http://php.net/json_decode) 매뉴얼 페이지는 여기에 당신을 도울 수 ... 그래서

관련 문제