Php阵列转置

时间:2015-12-29 06:38:16

标签: php

我有阵列名称grievance_pending它的值类似于" a"," b"," c"," d"是特定的部分和x,y,z,w是申诉解决级别我想显示申诉的状态所有部分的x,y,z和w有多少待处理 例如:数组值如a-x,a-y,a-z,a-w,b-x,c-x,c-y,d-w,e-x,e-y,e-w以相同的方式" a"可能有4个值或3个值或2个值或1个我想要显示的值 如下

          x y z w   total 
      a   0 1 2 0    3
      b   0 0 0 1    1
      c   0 5 0 1    6

请建议任何转置数组的方法

1 个答案:

答案 0 :(得分:0)

while($ row_count = mysql_fetch_array($ qry_count,MYSQL_ASSOC))          {

            if($x=="")
              {
                  if ($j>0)
                  {
                  $j++;
                  }
                 $x=$row_count['unitname'];
                 $unit_name[$j][0]=$x;
                 $unit_name[$j][1]=0;
                 $unit_name[$j][2]=0;
                 $unit_name[$j][3]=0;
                 $unit_name[$j][4]=0;               
                 raised_level($raised_level);            
               }
            else
              {
                      $row_count['unitname'];
                     if($x!=$row_count['unitname'])
                       {
                        $j++;
                             $unit_name[$j][0]=$row_count['unitname'];
                             $unit_name[$j][1]=0;
                             $unit_name[$j][2]=0;
                             $unit_name[$j][3]=0;
                             $unit_name[$j][4]=0;
                            raised_level($raised_level);
                             $x="";

                       }
                     else
                       {
                        raised_level($raised_level);
                       }

                }

    }

//功能提升等级             function raise_level($ raised_level)                 {

            switch($raised_level)
                                 {
                                  case "FHQrsBranch":
                                    $fhq_count=$fhq_count+$row_count['total_grievances'];
                                        $unit_name[$j][1]=$fhq_count;
                                        $fhq_count=0;

                                    break;

                                  case "Sector":
                                    $sector_count=$sector_count+$row_count['total_grievances'];
                                        $unit_name[$j][2]=$sector_count;
                                        $sector_count=0;

                                    break;

                                  case "Zone":
                                    $zone_count=$zone_count+$row_count['total_grievances'];
                                        $unit_name[$j][3]=$zone_count;
                                        $zone_count=0;
                                    break;

                                  case "Unit":
                                    $unit_count=$unit_count+$row_count['total_grievances'];
                                        $unit_name[$j][4]=$unit_count;
                                        $unit_count=0;
                                        break;                           
                                 }

            }