2

首先,我对 PHP 非常陌生,所以我仍在尝试。我的问题是:我试图在 PHP 中回显 Bing API 结果。

这是一个 JSON 结果示例,我从中获得

$jsonobj = json_decode($response);

(回复是我从 Bing 得到的,所以我只是粘贴了下面的回复 - 只需添加此信息,以防您想知道我从哪里得到$jsonobj = json_decode($response);

{"d":{"results":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Composite?Sources=\u0027web\u0027&Market=\u0027en-US\u0027&Query=\u0027php\u0027&Adult=\u0027off\u0027&$skip=0&$top=1","type":"ExpandableSearchResult"},"ID":"1c509d25-5ca4-4db5-bfc5-cafd6917e2c2","WebTotal":"10600000","WebOffset":"0","ImageTotal":"","ImageOffset":"","VideoTotal":"","VideoOffset":"","NewsTotal":"","NewsOffset":"","SpellingSuggestionsTotal":"","AlteredQuery":"","AlterationOverrideQuery":"","Web":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=0&$top=1","type":"WebResult"},"ID":"4cf2a8d6-21b7-433d-81e9-84e74091a44a","Title":"PHP: Hypertext Preprocessor","Description":"What is PHP? PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.","DisplayUrl":"www.php.net","Url":"http://www.php.net/"},{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=48&$top=1","type":"WebResult"},"ID":"2d8f8107-895e-4052-9edc-b656e74c3f2e","Title":"CakePHP: the rapid development php framework. Pages","Description":"Official website. Offers a manual for beginners and links towards the last version.","DisplayUrl":"cakephp.org","Url":"http://cakephp.org/"},{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=49&$top=1","type":"WebResult"},"ID":"816d781c-ff8b-4a60-b5b7-28d807bba28a","Title":"PHP Presents","Description":"Welcome to the PHP Presentation System. Here we list all of the available presentation categories stored within this system.","DisplayUrl":"talks.php.net","Url":"http://talks.php.net/"}],"Image":[],"Video":[],"News":[],"RelatedSearch":[],"SpellingSuggestions":[]}]}}

现在,我知道我可以使用以下命令来回显 WebTotal:

foreach($jsonobj->d->results as $value) {
    echo $value->WebTotal;
}

但是,我不知道如何回显实际结果,例如Title,DescriptionUrl.

我试过了:

foreach($jsonobj->d->results as $value) {
    echo $value->Title."<br>";
    echo $value->Description."<br>";
    echo $value->Url."<br>";
}

还有类似的东西:

foreach($jsonobj->d->results->Web as $value) {
    echo $value->Title."<br>";
    echo $value->Description."<br>";
    echo $value->Url."<br>";
}

因为我认为将 Web 添加到foreach遗嘱中可能会呼应正确的价值观,但没有成功。

也许有人可以帮助我并告诉我我做错了什么?

我的任务是获得以下结果:

标题:PHP:超文本预处理器

说明:什么是PHP?PHP 是一种广泛使用的通用脚本语言,特别适用于 Web 开发,并且可以嵌入到 HTML 中。

网址:http://www.php.net/

然后,其他两个结果。

太感谢了 :)

4

2 回答 2

5

您可以按如下方式执行此操作:

foreach($jsonobj->d->results as $result) {
    foreach($result->Web as $value) {
        echo $value->Title;
        // and the same for the other properties
    }
}

顺便说一句:你应该使用一个工具来格式化你的 json 字符串。这样会更清楚 json 数据的样子。

于 2012-10-01T21:04:38.437 回答
0

首先使用 print_r 方法,这样您就可以看到对象的外观,从而可以确定从哪里开始循环。

echo '<pre>';
print_r($jsonobj);

然后将类型更改为数组,如 $array = (array) $jsonobj; 所以你可以将它用于 array_shift 函数(你可以使用这个函数调整你的数组)

http://php.net/manual/en/function.array-shift.php

于 2012-10-01T21:05:12.603 回答