php - 如何使用 php 循环遍历该数组并在每一行中打印一个结果

标签 php arrays loops

如何循环遍历这个数组并在每一行中打印一个结果。这是由 google 搜索 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);

最佳答案

类似于:

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

关于php - 如何使用 php 循环遍历该数组并在每一行中打印一个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10257631/

相关文章:

php - sql pdo fetch问题可以解决它

PHP在保留键的同时获取关联数组的最后3个元素?

ios - 如何根据文本字段中的用户输入过滤字典数据数组

php - 如何对wp_query的结果进行排序

php - 登录页面不显示错误并且不登录 php

php - 如何从多维数组中删除一个数组?

php - 将十六进制值的字符串转换为 PHP 中 boolean 值的数组

c# - 用于查找数组中少数元素之和的 ParallelFor 代码(子集问题)

java - 为什么Java只在while循环的情况下才识别不可达代码?

java - 循环遍历 FXML 元素并将其中一些元素添加到列表时遇到问题