yeshang
V2EX  ›  问与答

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

  •  
  •   yeshang · Nov 12, 2011 · 5500 views
    This topic created in 5372 days ago, the information mentioned may be changed or developed.
    假如有一个数组,要求每输出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 replies    1970-01-01 08:00:00 +08:00
    pandasunny
        1
    pandasunny  
       Nov 12, 2011
    遍历或者直接分组array_chunk
    pandasunny
        2
    pandasunny  
       Nov 12, 2011
    我想想还是简单写一下,好久不抠腚会手生的。

    $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
       Nov 15, 2011
    @pandasunny 非常感谢啊.问题解决
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5377 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 66ms · UTC 07:16 · PVG 15:16 · LAX 00:16 · JFK 03:16
    ♥ Do have faith in what you're doing.