【问题标题】:Explode array to multi array将数组分解为多数组
【发布时间】:2018-05-24 04:10:16
【问题描述】:

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

您可以为此使用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

【讨论】:

    猜你喜欢
    • 2012-10-11
    • 2020-09-26
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    • 2016-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多