数组到无序列表 [英] Array to an unordered list

查看:160
本文介绍了数组到无序列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <?php $ b $我试图将一些数组的值映射到无序()列表。 b $ files = scandir($ dir); 
//删除。和..
print_r($ files);
?>

< ul>
<?php foreach($ files as $ file):?>
< li><?= $ file?>< / li>
<?php endforeach; ?>
< / ul>

它正确地遍历数组,因为它为< li> ; 元素。然而,这些项目符号旁边没有字符串输出。



输出看起来像是正确数量的项目符号,但旁边没有文本:







我在这里做错了什么?

解决方案

我不确定这件事,但这可能会起作用:

 <?php 
$ files = scandir($ dir);
//删除。和..
print_r($ files);
?>

< ul>
<?php foreach($ files as $ file){?>
< li><?php echo $ file; ?>< /锂>
<?php}?>
< / ul>

这就是我要用的,如果这不起作用,可能是因为你的 $ dir 变量不包含任何内容(有错误)。为什么你原来的代码可能没有工作的原因之一,因为我不认为< ?> 标签在每台服务器上都兼容。另外,据我所知,在php中没有<?= $ var?> 这个东西。我认为它只存在于ASP等。



编辑:在回答你关于花括号自卑的问题时,他们是在PHP中被普遍接受的标准。这在C / C ++ / C#系列中可能会有所不同,我不知道。


I'm trying to map some arrays values to an unordered () list.

    <?php
            $files = scandir($dir);
            //remove "." and ".."
            print_r($files);
    ?>

    <ul>
        <?php foreach($files as $file): ?>
            <li><?= $file ?></li>
        <?php endforeach; ?>
    </ul>

It does iterate through the array correctly as it gives bullets for <li> elements. However no string output is seen next to those bullets. Also when I print_r the array the values are there.

The output looks like this with the correct number of bullets but no text next to them:

.
.
.

What am I doing wrong here? Thanks in advance.

解决方案

I'm not sure about this, but this might work:

<?php
  $files = scandir($dir);
  //remove "." and ".."
  print_r($files);
?>

<ul>
  <?php foreach($files as $file) { ?>
  <li><?php echo $file; ?></li>
  <?php } ?>
</ul>

That's what I would use, and if this doesn't work, it might be because your $dir variable contains nothing (has an error). One reason why your original code might not have worked, because I don't think the <? ?> tags are compatible on every server. Also, from what I know, there is no <?=$var ?> thing in php. I thought it only exists in ASP and the like.

EDIT: In answer to your question about the inferiority of curly braces, they are the commonly accepted standard in PHP. This might be different in the C/C++/C# Family, I don't know.

这篇关于数组到无序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆