2

How can I loop through this array and print one result in each line. This is returned by google search api.

This works for me

print_r($rez->responseData);

But this doesn't

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

Below is the array

        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
        )

)

This is the code that generates this.

<?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);
3
  • Is it not possible to improve the format of this question. It is unreadable that way. Commented Apr 21, 2012 at 9:08
  • Please print_r your array and copy the result from the view source, and not the actual page. Place that here. Commented Apr 21, 2012 at 9:09
  • 1
    Read any basic primer on PHP? Commented Apr 21, 2012 at 9:29

1 Answer 1

2

Something like:

$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 />';
        }
    }
}

Or I didn't understand the question? Maybe you meant this?

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

foreach ($obj->result as $result)
{
    print_r($result);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Can you give me an example what exactly result do you need with current array?
I didn't need a code. I'm trying to understand what do you want to get as a result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.