将平面数组拆分为五个元素的子数组[重复]

问题描述 投票:0回答:1

这是我的默认数组,这个 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 
)

需要您的帮助来获取此输出数组

谢谢。

php arrays
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.