gpt4 book ai didi

显示 json 结果的 PHP 问题

转载 作者:行者123 更新时间:2023-12-02 22:27:56 25 4
gpt4 key购买 nike

首先,我对 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;
}

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

我试过:

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 可能会回显正确的值,但没有成功。

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

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

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.

URL:http://www.php.net/

然后,其他两个结果。

非常感谢:)

最佳答案

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

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

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

关于显示 json 结果的 PHP 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12680852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com