2012-04-21 2 views
2

この配列をループして各行に1つの結果を出力する方法を教えてください。これはGoogle検索APIによって返されます。この配列をループして、各行に1つの結果を出力する方法

これは私

print_r($rez->responseData); 

のために動作しますが、これは以下

print_r($rez->responseData->results); nor this print_r($rez->responseData['results']); 

ない。これは、これを生成するコードである配列

 stdClass Object 
(
    [results] => Array 
     (
      [0] => stdClass Object 
       (
        [GsearchResultClass] => GwebSearch 
        [unescapedUrl] => http://www.1860-1960.com/shoes.html 
        [url] => http://www.1860-1960.com/shoes.html 
        [visibleUrl] => www.1860-1960.com 
        [cacheUrl] => http://www.google.com/search?q=cache:4bB2OicXg5EJ:www.1860-1960.com 
        [title] => Beautiful <b>Antique Shoes</b> and Boots, Vintage Fashions 
        [titleNoFormatting] => Beautiful Antique Shoes and Boots, Vintage Fashions 
        [content] => <b>Vintage</b> Clothing. <b>Shoes</b> &amp; boots Hats &amp; bonnets. Parasols, purses, fans &amp; more. Textiles &amp; trims. New Items. ebay auctions. Find out how to order About us Click <b>...</b> 
       ) 

      [1] => stdClass Object 
       (
        [GsearchResultClass] => GwebSearch 
        [unescapedUrl] => http://www.ebay.com/sch/Pre1920-Edwardian-Older-/74977/i.html?_nkw=antique+shoes 
        [url] => http://www.ebay.com/sch/Pre1920-Edwardian-Older-/74977/i.html%3F_nkw%3Dantique%2Bshoes 
        [visibleUrl] => www.ebay.com 
        [cacheUrl] => http://www.google.com/search?q=cache:gKFzby7zoS0J:www.ebay.com 
        [title] => <b>antique shoes</b> | eBay 
        [titleNoFormatting] => antique shoes | eBay 
        [content] => eBay: <b>antique shoes</b>. <b>...</b> Quick Look. Antique 1905 Silk Satin Bridal Wedding Shoes Pumps Louis XV Heels Beautiful. Buy It Now $31.90 <b>...</b> 
       ) 

      [2] => stdClass Object 
       (
        [GsearchResultClass] => GwebSearch 
        [unescapedUrl] => http://www.amazon.com/Winsome-Wood-Shoe-Antique-Walnut/dp/B000NPQKHO 
        [url] => http://www.amazon.com/Winsome-Wood-Shoe-Antique-Walnut/dp/B000NPQKHO 
        [visibleUrl] => www.amazon.com 
        [cacheUrl] => http://www.google.com/search?q=cache:SBPbbjdSIpMJ:www.amazon.com 
        [title] => Amazon.com: Winsome Wood <b>Shoe</b> Rack, <b>Antique</b> Walnut: Home <b>...</b> 
        [titleNoFormatting] => Amazon.com: Winsome Wood Shoe Rack, Antique Walnut: Home ... 
        [content] => <b>Shoe</b> Rack. With Removable Sink tray, this unique product is nice to have it especially in wet weather.Warm Walnut finish <b>...</b> 
       ) 

      [3] => stdClass Object 
       (
        [GsearchResultClass] => GwebSearch 
        [unescapedUrl] => http://trouvais.com/category/antique-shoes/ 
        [url] => http://trouvais.com/category/antique-shoes/ 
        [visibleUrl] => trouvais.com 
        [cacheUrl] => http://www.google.com/search?q=cache:qPcTFS0bBR4J:trouvais.com 
        [title] => <b>Antique shoes</b> « Trouvais 
        [titleNoFormatting] => Antique shoes « Trouvais 
        [content] => 19th century silk <b>shoes</b>, and then stumbled upon. this tattered silk pair inscribed with a wedding date c1773. <b>antique</b> textiles. I&#39;m happy with just a few tangible <b>...</b> 
       ) 

     ) 

    [cursor] => stdClass Object 
     (
      [resultCount] => 11,400,000 
      [pages] => Array 
       (
        [0] => stdClass Object 
         (
          [start] => 0 
          [label] => 1 
         ) 

        [1] => stdClass Object 
         (
          [start] => 4 
          [label] => 2 
         ) 

        [2] => stdClass Object 
         (
          [start] => 8 
          [label] => 3 
         ) 

        [3] => stdClass Object 
         (
          [start] => 12 
          [label] => 4 
         ) 

        [4] => stdClass Object 
         (
          [start] => 16 
          [label] => 5 
         ) 

        [5] => stdClass Object 
         (
          [start] => 20 
          [label] => 6 
         ) 

        [6] => stdClass Object 
         (
          [start] => 24 
          [label] => 7 
         ) 

        [7] => stdClass Object 
         (
          [start] => 28 
          [label] => 8 
         ) 

       ) 

      [estimatedResultCount] => 11400000 
      [currentPageIndex] => 0 
      [moreResultsUrl] => http://www.google.com/search?oe=utf8&ie=utf8&source=uds&start=0&hl=en&q=antique+shoes 
      [searchResultTime] => 0.14 
     ) 

) 

です。

<?php 

/** 
* google_search_api() 
* Query Google AJAX Search API 
* 
* @param array $args URL arguments. For most endpoints only "q" (query) is required. 
* @param string $referer Referer to use in the HTTP header (must be valid). 
* @param string $endpoint API endpoint. Defaults to 'web' (web search). 
* @return object or NULL on failure 
*/ 
function google_search_api($args, $referer = 'http://localhost/test/', $endpoint = 'web'){ 
    $url = "http://ajax.googleapis.com/ajax/services/search/".$endpoint; 

    if (!array_key_exists('v', $args)) 
     $args['v'] = '1.0'; 

    $url .= '?'.http_build_query($args, '', '&'); 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    // note that the referer *must* be set 
    curl_setopt($ch, CURLOPT_REFERER, $referer); 
    $body = curl_exec($ch); 
    curl_close($ch); 
    //decode and return the response 
    return json_decode($body); 
} 


print_r($rez->responseData); 
+0

この質問の形式を改善することはできません。そんなことは読めません。 –

+0

あなたの配列を 'print_r'して、実際のページではなく**ビューソース**から結果をコピーしてください。ここに置きます。 –

+1

PHPの基本的なプライマーはどれですか? – liquorvicar

答えて

2

ような何か:

$obj = new obj(); // your object 

function printLies($obj) 
{ 
    foreach ($obj as $elem) 
    { 
     if (is_array($elem) || is_object($elem)) 
     { 
      printLies($obj); 
     } 
     else 
     { 
      echo $elem . '<br />'; 
     } 
    } 
} 

または私は質問を理解していませんでしたか?多分あなたはこれを意味しましたか?

$obj = new obj(); // your object 

foreach ($obj->result as $result) 
{ 
    print_r($result); 
} 
+0

この配列ではどのように動作しますか? – esafwan

+0

現在の配列で正確な結果が必要な場合の例を教えてください。 – Hast

+0

問題のコードを追加しました – esafwan

関連する問題