PHP列表结构

PHP列表结构,php,Php,假设我有一个包含以下项目的数组: $test = array(Item1, Item2, Item3, Item4, Item5, Item6, Item7, Item8, Item9); 如何使用for或foreach打印此结构 <ul> <li> <ul> <li>Item 1</li> <li>Item 2</li>

假设我有一个包含以下项目的数组:

$test = array(Item1, Item2, Item3, Item4, Item5, Item6, Item7, Item8, Item9);
如何使用for或foreach打印此结构

<ul>
    <li>
        <ul>
            <li>Item 1</li>
            <li>Item 2</li>
            <li>Item 3</li>
            <li>Item 4</li>
        </ul>
    </li>
    <li>
        <ul>
            <li>Item 5</li>
            <li>Item 6</li>
            <li>Item 7</li>
            <li>Item 8</li>
        </ul>
    </li>
    <li>
        <ul>
            <li>Item 9</li>
        </ul>
    </li>
</ul>
    • 项目1
    • 项目2
    • 项目3
    • 项目4
    • 项目5
    • 项目6
    • 项目7
    • 项目8
    • 项目9

您可以使用以下方法将数组拆分为块:

这将为您提供一个数组,其中每个数组包含4个项的子数组。然后,您可以循环执行这些操作以创建列表项:

<ul>
    <?php foreach ($chunks as $chunk): ?>
        <li>
            <ul>
                <?php foreach ($chunk as $item): ?>
                    <li><?php echo htmlspecialchars($item) ?></li>
                <?php endforeach ?>
            </ul>
        </li>
    <?php endforeach ?>
</ul>

您可以使用以下方法将数组拆分为块:

这将为您提供一个数组,其中每个数组包含4个项的子数组。然后,您可以循环执行这些操作以创建列表项:

<ul>
    <?php foreach ($chunks as $chunk): ?>
        <li>
            <ul>
                <?php foreach ($chunk as $item): ?>
                    <li><?php echo htmlspecialchars($item) ?></li>
                <?php endforeach ?>
            </ul>
        </li>
    <?php endforeach ?>
</ul>

使用数组块分割值

$test = array('Item1','Item2','Item3','Item4','Item5','Item6','Item7','Item8','Item9');

$chunk = array_chunk($test, 4);

echo '<ul>';
foreach($chunk as $pieces) {
    echo '<li>';
    echo '<ul>';
    foreach($pieces as $item) {
        echo '<li>'.$item.'</li>';
    }
    echo '</ul>';
    echo '</li>';
}
echo '</ul>';
$test=array('Item1','Item2','Item3','Item4','Item5','Item6','Item7','Item8','Item9');
$chunk=array\u chunk($test,4);
回声“
    ”; foreach($chunk作为$pieces){ 回音“
  • ”; 回声“
      ”; foreach(件为$item){ 回显“
    • ”.$item.“
    • ”; } 回声“
    ”; 回音“
  • ”; } 回声“
”;
使用数组块分割值

$test = array('Item1','Item2','Item3','Item4','Item5','Item6','Item7','Item8','Item9');

$chunk = array_chunk($test, 4);

echo '<ul>';
foreach($chunk as $pieces) {
    echo '<li>';
    echo '<ul>';
    foreach($pieces as $item) {
        echo '<li>'.$item.'</li>';
    }
    echo '</ul>';
    echo '</li>';
}
echo '</ul>';
$test=array('Item1','Item2','Item3','Item4','Item5','Item6','Item7','Item8','Item9');
$chunk=array\u chunk($test,4);
回声“
    ”; foreach($chunk作为$pieces){ 回音“
  • ”; 回声“
      ”; foreach(件为$item){ 回显“
    • ”.$item.“
    • ”; } 回声“
    ”; 回音“
  • ”; } 回声“
”;
试试这个

<?php
    $test = array(Item1, Item2, Item3, Item4, Item5, Item6, Item7, Item8, Item9);
    $ChunkedTestarray = array_chunk($test, 4);
    echo "<ul>";
    foreach($ChunkedTestarray as $Data) {
        echo "<li><ul>";
        for ($i = 0; $i <= count($Data)-1;) {
            echo "<li>".$Data[$i]."</li>";
            $i++;
        }
        echo "</ul></li>";
    }
    echo "</ul>";
?>

试试这个

<?php
    $test = array(Item1, Item2, Item3, Item4, Item5, Item6, Item7, Item8, Item9);
    $ChunkedTestarray = array_chunk($test, 4);
    echo "<ul>";
    foreach($ChunkedTestarray as $Data) {
        echo "<li><ul>";
        for ($i = 0; $i <= count($Data)-1;) {
            echo "<li>".$Data[$i]."</li>";
            $i++;
        }
        echo "</ul></li>";
    }
    echo "</ul>";
?>


你知道
用于
-和
-循环和
回音
吗?是的,我可以使用这个工具你知道
用于
-和
-循环和
回音
吗?是的,我也可以使用这个工具