V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
yeshang
V2EX  ›  问与答

php问题 如何每隔5次循环输出

  •  
  •   yeshang · 2011-11-12 13:14:34 +08:00 · 4613 次点击
    这是一个创建于 4561 天前的主题,其中的信息可能已经有所发展或是发生改变。
    假如有一个数组,要求每输出3个数据给套一个div,最后面不足3个的也套一个div

    $a=array(1,2,3,4,5,6,7,8);
    输出
    <div>1,2,3</div>
    <div>4,5,6</div>
    <div>7,8</div>

    关键是最后一个</div>如何加?
    3 条回复    1970-01-01 08:00:00 +08:00
    pandasunny
        1
    pandasunny  
       2011-11-12 13:18:21 +08:00
    遍历或者直接分组array_chunk
    pandasunny
        2
    pandasunny  
       2011-11-12 13:46:06 +08:00
    我想想还是简单写一下,好久不抠腚会手生的。

    $a=array(1,2,3,4,5,6,7,8);
    foreach(array_chunk($a, 3) as $v)
    {
    echo "<div>", implode(",", $v), "</div>\n";
    }
    yeshang
        3
    yeshang  
    OP
       2011-11-15 17:46:38 +08:00
    @pandasunny 非常感谢啊.问题解决
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   6204 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 02:46 · PVG 10:46 · LAX 19:46 · JFK 22:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.