我想从多行创建数组。在我的表格中,我有Year
字段,regd
字段用于计算学生数,以及Class
字段。我想输出如下数据。
$data = array(
'2012' => array(
'KG_I' => 87,
'KG_II' => 80,
'I' => 90,
'II' => 120,
'III' => 100,
'IV' => 110,
'V' => 98,
),
'2013' => array(
'KG_I' => 82,
'KG_II' => 84,
'I' => 92,
'II' => 110,
'III' => 120,
'IV' => 108,
'V' => 90,
),
'2014' => array(
'KG_I' => 90,
'KG_II' => 83,
'I' => 95,
'II' => 110,
'III' => 120,
'IV' => 81,
'V' => 95,
),
);
我的尝试是这样的:
$std=array();
$hms="SELECT COUNT(DISTINCT regd) as stdNum, Class, Year FROM
student GROUP by Class";
$quar=mysql_query($hms);
$myarray = array();
while($row = mysql_fetch_array($quar)){
$myarray[$row['Class']] = $row['stdNum'];
}
我不知道如何输出Year数组。
答案 0 :(得分:2)
我们试试这个:
while($row = mysql_fetch_array($quar)){
$myarray[$row['Year']][$row['Class']] = $row['stdNum'];
}
享受:)
答案 1 :(得分:0)
$std=array();
$hms="Select Year,Class,count(regd) 'stdNum' from student group by Year,Class";
$quar=mysql_query($hms);
$myarray = array();
while($row = mysql_fetch_array($quar)){
$myarray[$row['Year']]$row['Class']] = $row['stdNum'];
}
答案 2 :(得分:0)
您打开了两个不必要的数组。处理数组时print_r()
可能会派上用场。希望这会有所帮助。
$hms="Select Year, Class, COUNT(DISTINCT regd) as stdNum
from student group by Year, Class";
$quar=mysql_query($hms);
$myArray = array();
while($row = mysql_fetch_assoc($quar)){
$mya[$row['Year']][$row['Class']] = $row['stdNum'];
}
echo "<pre>";
print_r($myArray);
echo "</pre>";