2

我怎样才能遍历这个数组并在每一行打印一个结果。这是由谷歌搜索 api 返回的。

这对我有用

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);
4

1 回答 1

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);
}
于 2012-04-21T09:11:15.640 回答