我有一个得分表,我将按ID将该列分开 我的桌子是
我希望输出表会
我不知道在CodeIgniter中使用foreach的逻辑,谢谢 并尝试过
foreach ($data as $row) {
$records["list"][] = array(
$row['ID_STUDENT'],
$row['ID_TYPE'],
$row['SCORE'],
);
}
答案 0 :(得分:0)
这将为您构建所需的数组,尽管我建议您应编写查询以采用所需的格式检索数据。
$studentsScores = [
['idType'=>1, 'idStudent'=>1, 'score'=>'80'],
['idType'=>1, 'idStudent'=>2, 'score'=>'60'],
['idType'=>2, 'idStudent'=>3, 'score'=>'70'],
['idType'=>1, 'idStudent'=>4, 'score'=>'80'],
['idType'=>3, 'idStudent'=>1, 'score'=>'90'],
['idType'=>4, 'idStudent'=>2, 'score'=>'100'],
];
// Columns with default values
$columns = array_fill_keys(array_unique(array_column($studentsScores, 'idType')), 0);
$res = [];
foreach($studentsScores as $record){
if(!isset($res[$record['idStudent']])) $res[$record['idStudent']] = $columns;
$res[$record['idStudent']][$record['idType']] = $record['score'];
}
print_r($res);
希望这会有所帮助