Array to an unordered list

后端 未结 5 1860
一生所求
一生所求 2021-01-19 00:11

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

    

        
相关标签:
5条回答
  • 2021-01-19 00:48
    function array2ul($array) {
        $out = "<ul>";
        foreach($array as $key => $elem){
            if(!is_array($elem)){
                $out .= "<li><span>" . $key . ": " . $elem . "</span></li>";
            } else {
                $out .= "<li><span>" . $key . "</span>" . array2ul($elem) . "</li>";
            }
        }
        $out .= "</ul>";
        return $out; 
    }
    
    0 讨论(0)
  • 2021-01-19 00:54

    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.

    0 讨论(0)
  • 2021-01-19 01:05

    replace <?= $file ?> with <?php echo $file ?> or enable your sort tags by php.ini ; it seems your server has not it enabled.

    0 讨论(0)
  • 2021-01-19 01:10
    <?php foreach($files as $file): ?>
        <li><?php echo $file ?></li>
    <?php endforeach; ?>
    

    OR

    <?php foreach($files as $file): ?>
        <li><? echo $file ?></li>
    <?php endforeach; ?>
    
    0 讨论(0)
  • 2021-01-19 01:12

    Used this today:

    if ( !empty($files) ) echo '<li>' . implode('<li>', $files);
    
    0 讨论(0)
提交回复
热议问题