我如何在foreach中爆炸Group_Concat

时间:2017-12-05 17:26:18

标签: php html mysql

查询MySQL 这里查询以获取 Group_concat

的所有用户的结果
$result = $con->prepare("SELECT
    results.StuID,
    subjects_d.SubjectID,
    exams.ID,
    GROUP_CONCAT(results.ExamID,',',results.Exam1,',',results.Exam2,',',results.Exam3 ORDER BY results.ExamID  ASC) AS data
FROM
    results
LEFT JOIN
    users
ON 
    users.UID = results.StuID
LEFT JOIN
    subjects_d
ON
    subjects_d.SubID = results.Sub_ID
LEFT JOIN
    exams
ON 
    exams.ID = results.ExamID
WHERE 
    subjects_d.C_ID = 3
AND 
    subjects_d.SubjectID = 4
GROUP BY results.StuID");
$result->execute(array($classid, $subjectid));
$results = $result->fetchAll();

这是来自mysql的结果

PHP代码

<?php foreach($results as $result) { ?>
    <?php echo '<pre>';print_r($result)?>
<?php } ?>

的print_r

Array
(
    [StuID] => 70
    [0] => 70
    [SubjectID] => 4
    [1] => 4
    [ID] => 1
    [2] => 1
    [data] => 1,0,0,0,2,0,0,0,3,0,0,0,4,0,0,0
    [3] => 1,0,0,0,2,0,0,0,3,0,0,0,4,0,0,0
)

现在我如何在foreach循环中爆炸这个数据数组 谢谢

1 个答案:

答案 0 :(得分:1)

在foreach循环中使用逗号分隔数据字符串作为分隔符。

foreach($results as $result) {
    // explode with comma 
    $data_array = explode(',',$result['data']);
 }