php - Foreach - 包装
  • 里面有两个结果,然后重复。

    发布于 2024-12-01 04:17:05 字数 564 浏览 2 评论 0原文

    使用 php - 我试图在数组上运行“foreach”,但我想将每两个结果包装在 li 标签中。输出将如下所示。

  • 结果 1

    结果 2

    < /li>

  • 结果 3

    结果4

  • 结果5

    结果 6

    怎么办我去做这个吗?

    谢谢!

    Working with php -
    Im trying to run a "foreach" on an array, but i want to wrap every two results in li tags. Output will look like this.

    <li>

    <div>result 1</div>

    <div>result 2</div>

    </li>

    <li>

    <div>result 3</div>

    <div>result 4</div>

    </li>

    <li>

    <div>result 5</div>

    <div>result 6</div>

    </li>

    how do i go about doing this?

    Thanks!

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(3

    憧憬巴黎街头的黎明 2024-12-08 04:17:05
    $chunks = array_chunk($arr, 2);
    foreach ($chunks as $chunk) {
        // $chunk could have either 2 elements, or just one on the last iteration on an array with odd number of elements
        echo '<li>';
        foreach ($chunk as $value) {
            echo '<div>' . $value . '</div>';
        }
        echo '</li>';
    }
    
    $chunks = array_chunk($arr, 2);
    foreach ($chunks as $chunk) {
        // $chunk could have either 2 elements, or just one on the last iteration on an array with odd number of elements
        echo '<li>';
        foreach ($chunk as $value) {
            echo '<div>' . $value . '</div>';
        }
        echo '</li>';
    }
    
    夜声 2024-12-08 04:17:05
    $results = array(1, 2, 3, 4, 5, 6);
    echo "<li>";
    foreach($results as $pos => $result) {
        if ($pos > 2 && $pos % 2 == 0) {
            echo "</li>\n<li>";
        }
        echo "<div>result $result</div>";
    }
    echo "</li>";
    

    或者更简单地说:

    $results = array(1, 2, 3, 4, 5, 6);
    $max = count($results);
    for($i = 0; $i < $max; $i++) {
        echo "<li>";
        echo "<div>result " . $results[$i] . "</div>";
        $i++;
        echo "<div>result " . $results[$i] . "</div>";
        echo "</li>";
    }
    
    $results = array(1, 2, 3, 4, 5, 6);
    echo "<li>";
    foreach($results as $pos => $result) {
        if ($pos > 2 && $pos % 2 == 0) {
            echo "</li>\n<li>";
        }
        echo "<div>result $result</div>";
    }
    echo "</li>";
    

    or more simply:

    $results = array(1, 2, 3, 4, 5, 6);
    $max = count($results);
    for($i = 0; $i < $max; $i++) {
        echo "<li>";
        echo "<div>result " . $results[$i] . "</div>";
        $i++;
        echo "<div>result " . $results[$i] . "</div>";
        echo "</li>";
    }
    
    度的依靠╰つ 2024-12-08 04:17:05
    $count = 0;
    foreach ($array as $key=>$value) {
        ++$count;
        if ($count == 1) {
            echo "<li>";
            echo "<div>" . $value."</div>";    
        } else {
            echo "<div>" . $value."</div>";    
            echo "</li>";
            $count = 0;
        }
    }
    
    $count = 0;
    foreach ($array as $key=>$value) {
        ++$count;
        if ($count == 1) {
            echo "<li>";
            echo "<div>" . $value."</div>";    
        } else {
            echo "<div>" . $value."</div>";    
            echo "</li>";
            $count = 0;
        }
    }
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文