简体   繁体   English

遍历特定数组元素

[英]Looping through specific array elements

I have a table and each row contains, among other things, 5 columns which may or may not contain an image file name. 我有一张桌子,每行除其他外,还包含5列,这些列可能包含也可能不包含图像文件名。 Let's say I've retrieved that row and put it into an assoc array. 假设我已经检索了该行并将其放入一个assoc数组中。 I want to loop through and echo those image file names (cols may or may not all be populated) into html tags, but only if that column has an image file name in it. 我想遍历并回显那些图像文件名(cols可能会或可能不会全部填充)到html标签中,但前提是该列中有图像文件名。 Is there a better way to do it than this? 有没有比这更好的方法了?

for ($i = 1; $i < 6; $i++){
if($item_array['image_' . {$i}]{
echo "<li><img src=\"images/work-items/$item_array['image_' . {$i} . '.jpg'\"/></li>"
}  

Your syntax was all over the place: 您的语法无处不在:

for ($i = 1; $i < 6; $i++){
    if(isset($item_array['image_' . $i])){
        echo '<li><img src="images/work-items/'. $item_array['image_' . $i] . '.jpg"/></li>';
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM