duanliao2310 2014-09-01 09:46 采纳率: 100%
浏览 19
已采纳

如何为此做好循环?

I have this array :

$values = Array
(
    [0] => foo-1
    [1] => foo-2
    [2] => foo-3
    [3] => foo-4
    [4] => foo-5
    [5] => foo-6
    [6] => foo-7
    [7] => foo-8
    [8] => foo-9
    [9] => foo-10
    [10] => foo11
       [...]
    [30] => foo-31
    [31] => foo-32
)

I need to explode the array all the 13 values, and get this :

echo "foo-1, foo-2, foo-3, foo-4, foo-5, foo-6 [...], foo-13";

echo "foo-14, foo-15, foo-16, foo-17 [...], foo-26";

echo "foo-27, foo-28, foo-29, foo-30, foo-31, foo-32";

So, this is my code :

$nVal = count($values);
$nbTab = $nVal / 13;
$nbTab = round($nbTab, 0, PHP_ROUND_HALF_UP);
for ($cpt = 0; $cpt <= $nbTab-1; $cpt++) {
$line = "";
if($cpt == 0)
 $cptEntete = $cpt*12;
else
 $cptEntete = ($cpt*12)+1;
echo "Cpt entete :".$cptEntete;

if($cpt+1 == $nbTab){
 $jusque = $nVal;
}
else{
 if($cpt == 0)
  $jusque = 12;
 else
  $jusque = 12*($cpt+1);
}

for($cptEntete;$cptEntete <= $jusque;$cptEntete++){
 $line.= $cptEntete." ,";
}

echo $line."<br />";

The problem is that I don't arrive to obtain good lines with my 13 values by line :(

Can help me please ?

  • 写回答

3条回答 默认 最新

  • doufunuo4787 2014-09-01 09:50
    关注

    Just try with:

    $values = array( /* your data */ );
    $parts = array_chunk($values, 13);
    
    foreach ($parts as $part) {
        echo implode(', ', $part);
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal