我有一个多维数组:
$carousel = array(
array(
"cite" => 'title_1',
"blockquote" => 'description_1',
"imgs" => 4
),
array(
"cite" => 'title_2',
"blockquote" => 'description_2',
"imgs" => 2
)
);
我想将此信息包装为html并回显n个图像作为imgs值。 我只能回显n张图像,但我想分别将引用和块引用关联起来。
我尝试使用foreach但没有成功:
foreach ($carousel as $images){
foreach ($images as $key => $value){
echo $value . "\n";
for ($i = 0; $i < $value; $i++) {
echo("image url" . "\n\n");
}
}
}
我在回显$ value和图像时获得所有值:
title_1
description_1
4
image url
image url
image url
image url
title_2
description_2
2
image url
image url
但是我想像这样单独使用它们
<cite>title_1</cite>
<blockquote>description_1</blockquote>
<img></img> //4 times as specified in "imgs"
<img></img>
<img></img>
<img></img>
<cite>title_2</cite>
<blockquote>description_2</blockquote>
<img></img> //2 times as specified in "imgs"
<img></img>
我很感谢任何建议。
答案 0 :(得分:4)
您有一个不需要的额外的foreach循环。
尝试一下:
//loop through array
foreach($carousel as $values) {
//echo cite info
echo "<cite>{$values['cite']}</cite>";
//echo blockquote info
echo "<blockquote>{$values['blockquote']}</blockquote>";
//loop until you have the same amount of <img> tags as defined in the `imgs` array element.
for ($x = 1; $x <= $values['imgs']; $x++) {
echo "<img></img>";
}
}
答案 1 :(得分:-1)
尝试一下:
foreach ($carousel as $singleitem){
echo "<cite>".$singleitem['cite']."</cite>
<blockquote>".$singleitem['blockquote']."</blockquote>";
for ($i = 0; $i < $singleitem['imgs']; $i++) { //loop through img
echo("image url" . "<br/>");
}
}