foreach在x次后做什么? [英] Foreach do something after x times?

查看:139
本文介绍了foreach在x次后做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个自动块与链接[他们来自一个数组],
一切顺利,在我有太多的链接,现在他们都在同一行,

I'm trying to create an automatic block with links [ They come from an array ], everything went well before I had too much links, Now they're all on the same line, How can I make foreach print br after 4 times?

现在是这样:

foreach($this->rpanelinks as $name => $url) {
    echo '<a href="' . BASE_URL . $url . '">' . $name . '</a>';
}

谢谢!

推荐答案

使用计数器:

$i = 1;

foreach($this->rpanelinks as $name => $url) {
    if($i == 4) 
        echo '<br>';

    echo '<a href="' . BASE_URL . $url . '">' . $name . '</a>';

    ++$i;
}

或如果您不每次 4次

$i = 1;

foreach($this->rpanelinks as $name => $url) {
    if($i % 4 == 0) 
         echo '<br>';

    echo '<a href="' . BASE_URL . $url . '">' . $name . '</a>';

    ++$i;
}

$ i%4 计算其余的操作 $ i / 4 ,如果它 0 ,值uf $ i 可由 4 分割。

$i % 4 calculates the rest of the operation $i / 4 and if it's 0 the value uf $i is dividable by 4.

这篇关于foreach在x次后做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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