Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按数组的键名对数组进行分组。[关键字名称取决于用户的输入]_Php_Mysql_Arrays - Fatal编程技术网

Php 按数组的键名对数组进行分组。[关键字名称取决于用户的输入]

Php 按数组的键名对数组进行分组。[关键字名称取决于用户的输入],php,mysql,arrays,Php,Mysql,Arrays,这是我生成数组的代码 $query = "SELECT GROUP_CONCAT(distinct SubCode ORDER BY SubCode SEPARATOR ' ')as SubCode,GROUP_CONCAT(grade ORDER BY SubCode SEPARATOR ' ')as grade FROM VMESubjectGrade where ProgCode='$a' and Sessi='$b' and Intake='$c' GR

这是我生成数组的代码

$query = "SELECT GROUP_CONCAT(distinct SubCode ORDER BY SubCode SEPARATOR ' ')as           
SubCode,GROUP_CONCAT(grade ORDER BY SubCode SEPARATOR ' ')as grade FROM VMESubjectGrade      
where ProgCode='$a' and Sessi='$b' and Intake='$c' GROUP BY matricNo";

$cols = "SELECT GROUP_CONCAT(distinct SubCode ORDER BY SubCode SEPARATOR ' ')as   
SubCode,sub_id FROM VMESubjectGrade where ProgCode='$a' and Sessi='$b' and 
Intake='$c' order by SubCode" ;


$result = mysql_query($cols); 


$gradee = mysql_query($query);


while ($row1 = mysql_fetch_assoc($result))

{  $pixx =explode(" ", $row1['SubCode']);

} 


while($row = mysql_fetch_array($gradee))

{
$pieces =explode(" ", $row['SubCode']);
$comps = array_intersect($pixx, $pieces);
$pi =explode(" ", $row['grade']);
$conbi = array_combine ($comps ,$pi );

//print_r($conbi);

echo array2table($conbi);
 }
`array2table` is a function that produce table for any array
这是我的阵列

conbi = 

Array ( [MMB617] => I )

Array ( [MBP633] => B+ [MMB617] => C- [MQM613] => A- )

Array ( [MCT654] => A- [MMB617] => B- [MTH651] => B+ )

Array ( [MPP799] => A- ) 

Array ( [MMB617] => I [MMD634] => I [MQM613] => I ) 

Array ( [MBP633] => A- [MVF632] => A ) Array ( [MPP799] => B ) 

Array ( [MMB617] => I [MQM613] => I )
当我按表输出时,它是这样的

MMB617
I
MBP633         MMB617           MQM613
B+              C-                  A-
MCT654         MMB617           MTH651
A-               B-                 B+
MPP799
A-
MMB617         MMD634            MQM613
 I               I                I
MBP633         MVF632
 A-              A
MPP799
 B
MMB617         MQM613
 I               I
MBP633  MCT654  MMB617  MMD634  MPP799  MQM613  MTH651  MVF632
                    I
B+                  C-                       A-
            A-      B-                             B+
                            A-
                    I       I                I
A-                                                        A
                                    B
                    I                        I
我希望我的数组按主题分组,这样所有的主题都是标题,我的表如下所示

MMB617
I
MBP633         MMB617           MQM613
B+              C-                  A-
MCT654         MMB617           MTH651
A-               B-                 B+
MPP799
A-
MMB617         MMD634            MQM613
 I               I                I
MBP633         MVF632
 A-              A
MPP799
 B
MMB617         MQM613
 I               I
MBP633  MCT654  MMB617  MMD634  MPP799  MQM613  MTH651  MVF632
                    I
B+                  C-                       A-
            A-      B-                             B+
                            A-
                    I       I                I
A-                                                        A
                                    B
                    I                        I

你的问题不清楚,你的代码也不清楚。试试这个

$ordered = array()
foreach ($conbi as $arrays) {
    foreach($arrays as $key=>$value){
       if(!array_key_exists($key, $ordered){$ordered[$key] = array();}
       array_push($ordered[$key], $value);
    }
}

print_r($ordered);

为了得到好的或任何答案,你最好提出清楚的问题。环顾四周看看好的例子。

你可能想清理你的帖子,代码和问题很难阅读,上下文也没有正确定义。清理完我的法语我不清楚你的数组是什么样子。当我打印r$combi时,它看起来就像这个数组([MMB617]=>i)数组([MBP633]=>B+[MMB617]=>C-[MQM613]=>A-)数组([MCT654]=>A-[MMB617]=>B-[MTH651]=>B+数组([MPP799]=>A-)数组([MMB617]=>I[MMD634]=>I[MQM613]=>I)数组([MBP633]=>A-[MVF632]=>A)数组([MPP799]=>B)数组([MMB617]=>I[MQM613]=>I)您应该将您的
打印到html代码中,以便于阅读