如何在if-else语句中获取PHP中的对象JSON?
我想设置图像,但在第8行中有错误的
<?php
$url = "https://dog.ceo/api/breeds/image/random";
$json = file_get_content($url);
$obj = json_decode($json, TRUE);
foreach($obj as $key) if
(
echo <img src='$key['message']'/>
else
echo <img src='not_found.png'/>
);
?>
解决方案是什么?
i want to setup image but got error in line 8
<?php
$url = "https://dog.ceo/api/breeds/image/random";
$json = file_get_content($url);
$obj = json_decode($json, TRUE);
foreach($obj as $key) if
(
echo <img src='$key['message']'/>
else
echo <img src='not_found.png'/>
);
?>
what's the solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(1)
当API返回常规JSON时,您不需要
foreach
循环。也许您想获得这样的结果。You don't need a
foreach
loop as the api returns regular json. Perhaps you wanted to get such a result.