2013-07-08 2 views
3

나는 이것이 아마도 단순한 것으로 알고 있으며 나는 이것에 관해 어떻게해야하는지 이해하기 시작했다. 이것이 내가 OO와 관련이 있다고 생각한다. OO 및 json 메신저에이 약간의 문제가있는 http://api.discogs.com/database/search?q=d&page=1&per_page=5 그것은 PHP 함수와 구문 분석 할 수 있도록 d 쿼리를 5 결과를 반환하고 다음 첫 번째 결과에 대한 thubmnail 표시 내 말을 해봅시다 코드 :치명적인 오류 : stdClass 타입의 객체를 배열로 사용할 수 없다.

$returnData = file_get_contents($queryURL); 
$discogsJSON = json_decode($returnData); 

여기서 $queryURL = 제공된 URL. 내가 $discogsJSON를 인쇄하면 내가이 얻을 : 정확히 올바른이다

<pre>stdClass Object 
(
[pagination] => stdClass Object 
    (
     [per_page] => 5 
     [items] => 179465 
     [page] => 1 
     [urls] => stdClass Object 
      (
       [last] => http://api.discogs.com/database/search?q=f&per_page=5&page=35893 
       [next] => http://api.discogs.com/database/search?q=f&per_page=5&page=2 
      ) 

     [pages] => 35893 
    ) 

[results] => Array 
    (
     [0] => stdClass Object 
      (
       [style] => Array 
        (
         [0] => Free Jazz 
        ) 

       [thumb] => http://api.discogs.com/image/R-90-1672955-1305426452.jpeg 
       [format] => Array 
        (
         [0] => Vinyl 
         [1] => LP 
         [2] => Album 
        ) 

       [country] => Switzerland 
       [title] => Steve Lacy - Clinkers 
       [uri] => /Steve-Lacy-Clinkers/master/54331 
       [label] => Array 
        (
         [0] => Hat Hut Records 
        ) 

       [catno] => F 
       [year] => 1978 
       [genre] => Array 
        (
         [0] => Jazz 
        ) 

       [resource_url] => http://api.discogs.com/masters/54331 
       [type] => master 
       [id] => 54331 
      ) 

     [1] => stdClass Object 
      (
       [style] => Array 
        (
         [0] => Free Jazz 
        ) 

       [thumb] => http://api.discogs.com/image/R-90-1672955-1305426452.jpeg 
       [format] => Array 
        (
         [0] => Vinyl 
         [1] => LP 
         [2] => Album 
        ) 

       [country] => Switzerland 
       [title] => Steve Lacy - Clinkers 
       [uri] => /Steve-Lacy-Clinkers/release/1672955 
       [label] => Array 
        (
         [0] => Hat Hut Records 
        ) 

       [catno] => F 
       [year] => 1978 
       [genre] => Array 
        (
         [0] => Jazz 
        ) 

       [resource_url] => http://api.discogs.com/releases/1672955 
       [type] => release 
       [id] => 1672955 
      ) 

     [2] => stdClass Object 
      (
       [style] => Array 
        (
         [0] => Alternative Rock 
         [1] => Goth Rock 
         [2] => Heavy Metal 
        ) 

       [thumb] => http://api.discogs.com/image/R-90-2785283-1300918365.jpeg 
       [format] => Array 
        (
         [0] => CD 
         [1] => Compilation 
         [2] => Unofficial Release 
        ) 

       [country] => Russia 
       [title] => HIM (2) - Overload Hits 
       [uri] => /HIM-Overload-Hits/release/2785283 
       [label] => Array 
        (
         [0] => FullHouse Records 
        ) 

       [catno] => F????? 
       [year] => 2003 
       [genre] => Array 
        (
         [0] => Rock 
        ) 

       [resource_url] => http://api.discogs.com/releases/2785283 
       [type] => release 
       [id] => 2785283 
      ) 

     [3] => stdClass Object 
      (
       [style] => Array 
        (
         [0] => Drone 
         [1] => Ambient 
        ) 

       [thumb] => http://api.discogs.com/image/R-90-3108621-1316178067.jpeg 
       [format] => Array 
        (
         [0] => CDr 
         [1] => Album 
         [2] => Limited Edition 
        ) 

       [country] => Russia 
       [title] => Solar Ocean - Twillight October 
       [uri] => /Solar-Ocean-Twillight-October/release/3108621 
       [label] => Array 
        (
         [0] => Necrophone 
        ) 

       [catno] => F 
       [year] => 2010 
       [genre] => Array 
        (
         [0] => Electronic 
        ) 

       [resource_url] => http://api.discogs.com/releases/3108621 
       [type] => release 
       [id] => 3108621 
      ) 

     [4] => stdClass Object 
      (
       [style] => Array 
        (
         [0] => Techno 
         [1] => Electro 
         [2] => Tech House 
         [3] => Minimal 
        ) 

       [thumb] => http://api.discogs.com/image/R-90-699054-1161976299.jpeg 
       [format] => Array 
        (
         [0] => Vinyl 
         [1] => 12" 
         [2] => Box Set 
         [3] => Compilation 
         [4] => Limited Edition 
        ) 

       [country] => Germany 
       [title] => Various - Cocoon Compilation F 
       [uri] => /Various-Cocoon-Compilation-F/master/33655 
       [label] => Array 
        (
         [0] => Cocoon Recordings 
         [1] => Cocoon Compilation 
        ) 

       [catno] => COR LP 011 
       [year] => 2006 
       [genre] => Array 
        (
         [0] => Electronic 
        ) 

       [resource_url] => http://api.discogs.com/masters/33655 
       [type] => master 
       [id] => 33655 
      ) 

    ) 

) 
</pre> 

을 그리고 난 내가 액세스하려고하지만 내가 원하는 모든 데이터를 볼 수 있도록처럼 (첫 번째의 썸네일을 말할 수) :

올바른 에코 문을 구성 떨어져 갈 수있는 충분한 정보가 여기에서 가정 Fatal error: Cannot use object of type stdClass as array

하지만, 사람이를 얻을 수있는 올바른 방법 뒤에 나에게 OO 이론을 설명 할 수

echo $discogsJSON['results']['0']['thumb']; 

내가 왼쪽입니다 데이터 아웃?

답변

14

$discogsJSON은 배열이 아니지만 stdClass 개체입니다. results는이 객체의 속성입니다.

귀하가 찾고자하는 것을해야합니다.

echo $discogsJSON->results['0']->thumb; 

그러나, 반환 값이 연관 배열이 있는지 확인 json_decodetrue 플래그를 추가하는 것이 더 convinient 수 있습니다.

$discogsJSON = json_decode($returnData, true); 
+1

당신의 선생님이 이번 주 인터넷에 내 faovrite 사람이다 <3 – Nick

+0

감사의 두 번째 매개 변수로 사실을 추가하여 배열 연관을해야 당신! 그것이 만족 스럽다면 답변을 수락하십시오. – apartridge

+0

가는 6 분 – Nick

1

당신은 json_decode

$discogsJSON = json_decode($returnData, true);

관련 문제