将数组分解为多数组

时间:2017-12-10 13:29:49

标签: php arrays

这是我的默认数组,此concat_group将在我的代码中爆炸

MySQL代码 这是我获取数据的查询

SELECT
    results.StuID,
    subjects_d.SubjectID,
    subjects.ID,
    users.*,
    exams.ID,
    classes.Name AS ClName,
    GROUP_CONCAT(subjects.Super_Degree, ',',subjects_d.SubjectID, ',', results.ExamID, ',',results.Exam1,',',results.Exam2,',',results.Exam3 ORDER BY subjects_d.SubjectID, 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
    subjects
ON
    subjects_d.SubjectID = subjects.ID
LEFT JOIN
    exams
ON 
    exams.ID = results.ExamID
INNER JOIN
    classes
ON 
    classes.CID = subjects_d.C_ID
WHERE 
    subjects_d.C_ID = ?
GROUP BY results.StuID
ORDER BY StuID

PHP代码 在这里我想从MySQL中爆炸我的数组

<?php foreach ($arr as $student) {
    <?php $data_array = explode(',',$student['data']); ?>
    <?php echo '<pre>'; print_r($data_array) ?>
<?php } ?>

输出 print_r的输出。

Array
(
    [0] => 1
    [1] => 90
    [2] => 0
    [3] => 0
    [4] => 0
    [5] => 2
    [6] => 90
    [7] => 0
    [8] => 0
    [9] => 0
    [10] => 3
    [11] => 90
    [12] => 0
    [13] => 0
    [14] => 0 
}

我需要将它分解为像这样的多数组

需要获得此输出

Array[0]
(
    [0] => 1
    [1] => 90
    [2] => 0
    [3] => 0
    [4] => 0
)
Array[1]
    (
    [5] => 2
    [6] => 90
    [7] => 0
    [8] => 0
    [9] => 0
)
Array[2]
    (
    [10] => 3
    [11] => 90
    [12] => 0
    [13] => 0
    [14] => 0 
)

需要你的帮助来获得这个输出数组

谢谢。

1 个答案:

答案 0 :(得分:3)

您可以使用array_chunk

$final = array_chunk( $arr, 5, true ); 

这将导致:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 90
            [2] => 0
            [3] => 0
            [4] => 0
        )

    [1] => Array
        (
            [5] => 2
            [6] => 90
            [7] => 0
            [8] => 0
            [9] => 0
        )

    [2] => Array
        (
            [10] => 3
            [11] => 90
            [12] => 0
            [13] => 0
            [14] => 0
        )

)

文档:http://php.net/manual/en/function.array-chunk.php