首页 > 解决方案 > 使用循环对数据中的项目和计数进行分组

问题描述

我有数据时

var_dump ($data);

这是我的数据的显示方式($data 不是表格应答器):

+-------+----------+-----------+---------------+
| adan  | Afla     | avo       | 0             |
+-------+----------+-----------+---------------+
| adan  | ken      | joly      | 0             |
+-------+----------+-----------+---------------+
| busia | koko     | ho        | 0             |
+-------+----------+-----------+---------------+
| busia | koko     | ho        | 0             |
+-------+----------+-----------+---------------+
| busia | koko     | ho        | 0             |
+-------+----------+-----------+---------------+

但是,我希望表格显示变为:

+-------+----------+-----------+---------------+
| adan  | Afla     | avo       | 1             |
+-------+----------+-----------+---------------+
| adan  | ken      | joly      | 1             |
+-------+----------+-----------+---------------+
| busia | koko     | ho        | 3             |
+-------+----------+-----------+---------------+

这是我的代码:

foreach($data as $liste){
    $town[]=$liste->getLocalite()->getName();
    $counties[]=$liste->getCountie()->getName();
    $districts[]=$liste->getDistrict()->getName();;
}

$a= count($listforages)-1;
$numberDoc=0;
$tab1=$tab2=$tab3=$tab4=array();


    for ($i=0; $i<count($data);$i++){

$tab[$i]= $town[$i].'--'.$counties [$i].'--'.$districts[$i].'--'.$numberDoc[$i];

if( $i == $a  ) {

     if ($counties [$a-1]==$counties [$a] && $districts[$a-1]==$districts[$a]  && $numberDoc[$a-1]==$numberDoc[$a] ){
        $numberDoc[$a]++; //  counter
        $tab1[$a-1]=$town[$a-1].'--'.$counties [$a-1].'--'.$districts[$a-1].'--'. $numberDoc[$a-1];
     }

     if ($counties [$a-1] !=$counties [$a] && $districts[$a-1] !=$districts[$a]  && $numberDoc[$a-1] !=$numberDoc[$a] ){
    $numberDoc[$a]++; //  counter
      $tab2[$a-1]=$town[$a-1].'--'.$counties [$a-1].'--'.$districts[$a-1].'--'. $numberDoc[$a-1];
     }
}   
 else{
    if ($counties [$i]==$counties [$i+1] && $districts[$i]==$districts[$i+1]  && $numberDoc[$i]==$numberDoc[$i+1] ){
      $numberDoc[$i]++; //  counter
      $tab3[$i]=$town[$i].'--'.$counties [$i].'--'.$districts[$i].'--'. $numberDoc[$i];
     }

    if ($counties [$i] !=$counties [$i+1] && $districts[$i] !=$districts[$i+1]  && $numberDoc[$i] !=$numberDoc[$i+1] ){
        $numberDoc[$i]++; //  counter
      $tab4[$i]=$town[$i].'--'.$counties [$i].'--'.$districts[$i].'--'. $numberDoc[$i];
     }
}



}

当你看到我的数据时(见表格):

town 是第 0 列,counties 是第 1 列,区是第 2 列,numberDoc 是第 3 列,

我的代码不起作用,如何解决这个问题?

注意:原谅我的英语,

先感谢您

标签: phpsymfony

解决方案


推荐阅读