我正在使用 php 遍历一个 json 对象,并尝试将 json 对象中的图像显示为 div 的背景图像。
我的 json 对象如下所示:
{
"1.49514754373E+12": {
"description": "This is amazing!",
"fileNames": [
"3.jpg"
],
}
}
我用来在 html 中显示图像的 php 文件如下所示
<?php
$json = file_get_contents('auction.json');
$json = json_decode($json);
foreach($json as $obj){
echo "<p>" .$obj->description . "</p>";
//loop through the array of images
for ($x=0; $x < count($obj->fileNames); $x++) {
echo "<div style='background-image: url('" .$obj->fileNames[$x]."');'>";
}
}
?>
我遇到的问题是它正在像这样设置 html
<div style="background-image: url(" 3.jpg');'></div>
谁能看到我做错了什么?
我用双引号包裹了样式值并用\
.
echo "<div style=\"background-image: url('" . $obj->fileNames[$x] . "');\">";
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句