【发布时间】:2015-06-05 17:57:23
【问题描述】:
为了只获取 php 脚本的特定部分,我使用了一个带有与 id 相关的条件的 foreach,因此它给出了一个作用域结果。我现在遇到的问题是如何访问我拥有的信息......我会展示一些例子来帮助说明我的困惑
listCheck.innerHTML = "<img src = " + "./images/" + json[i].exhibits[j].exhibit_image + " Photo Cover' height='200' width='200'>";
在访问 json 之后,它会使用标签 Exhibition_image 获取适当的数据。然而,使用 ForEach 调整此方法会导致错误,因为我认为代码不知道我要求什么,我认为这是语法错误
这是我尝试过的代码...
function getNewYork()
{
myExhibitionsView = document.getElementById('exhibitioncontent');
images = document.createElement('ul');
json.forEach( function(element) {
if( element['exhibition_id'] == 1 ){
console.log(element);
for (var i = 0; i < element.length; i++) {
for (var j = 0; j < element[i].exhibits.length; j++) {
list = document.createElement('p');
list.id = 'image';
list.innerHTML = "<img src = " + "./images/" + element[i].exhibits[j].exhibit_image + " Photo Cover' height='200' width='200'>";
console.log(list);
myExhibitionsView.appendChild(images);
images.appendChild(list);
}
}
};
});
}
我正在尝试从这组 json 中提取展览图片 url
array("exhibition_id" => "1", "exhibition_title" => "New York, New York", "exhibition_subject" => "New York", "ticket_price" => "10",
"exhibits" => array(
array("exhibit_id" => "3", "exhibit_title" => "Brooklyn Bridge from City Hall Park", "exhibit_description" => "New York, June 2005", "exhibit_image" => "brooklynbridge.jpg", "photographer" => "MLG"),
array("exhibit_id" => "6", "exhibit_title" => "Central Park, New York", "exhibit_description" => "New York, June 2005", "exhibit_image" => "centralpark.jpg", "photographer" => "MLG"),
array("exhibit_id" => "7", "exhibit_title" => "Chrysler Building at night, New York", "exhibit_description" => "New York, July 2001", "exhibit_image" => "chrysler_building.jpg", "photographer" => "MLG")
),
"locations" => array(
array("location_id" => "1", "location_name" => "Kelvingrove Art Gallery and Museum", "location_postcode" => "G3 8AG"),
array("location_id" => "3", "location_name" => "Walker Art Gallery", "location_postcode" => "L3 8EL"),
array("location_id" => "5", "location_name" => "Tate Modern", "location_postcode" => "SE1 9TG")
)
),
执行此操作的正确语法是什么?感谢您的帮助
【问题讨论】:
标签: javascript php json foreach