如何将数组添加到数组中以成为二维数组?

时间:2015-09-14 01:59:50

标签: php arrays

我从数据库中检索数据,数据包含多行"Semester", "TimeTableID", "SubjectID", "SubjectName", "FacultyName", "Day", "StartTime", "EndTime", "Location" 当我获得数据时,我想按"Semester"对它们进行分组 例如,我从数据库中检索了这些数据:

Array
(
    [0] => Array
        (
            [Semester] => Semester 1, 2015
            [TimeTableID] => 11
            [SubjectID] => CSCI2203
            [SubjectName] => VISUAL PROGRAMMING
            [FacultyName] => NAME 0
            [Day] => Tue&Thu
            [StartTime] => 10:15:00
            [EndTime] => 12:00:00
            [Location] => C4
        )

    [1] => Array
        (
            [Semester] => Semester 1, 2015
            [TimeTableID] => 12
            [SubjectID] => CSCI2903
            [SubjectName] => FUNDAMENTALS OF NETWORKING
            [FacultyName] => NAME 1
            [Day] => Fri
            [StartTime] => 10:15:00
            [EndTime] => 12:00:00
            [Location] => C13
        )

    [2] => Array
        (
            [Semester] => Semester 1, 2015
            [TimeTableID] => 10
            [SubjectID] => CSCI2003
            [SubjectName] => DATA STRUCTURES
            [FacultyName] => NAME 2
            [Day] => Mon&Wed
            [StartTime] => 10:15:00
            [EndTime] => 12:00:00
            [Location] => C6
        )

    [3] => Array
        (
            [Semester] => Semester 2, 2015
            [TimeTableID] => 5
            [SubjectID] => CSCI2903
            [SubjectName] => FUNDAMENTALS OF NETWORKING
            [FacultyName] => NAME 3
            [Day] => Fri
            [StartTime] => 08:00:00
            [EndTime] => 09:45:00
            [Location] => LH3
        )

    [4] => Array
        (
            [Semester] => Semester 2, 2015
            [TimeTableID] => 3
            [SubjectID] => CSCI3003
            [SubjectName] => INTRODUCTION TO SOFTWARE ENGINEERING
            [FacultyName] => NAME 1
            [Day] => Mon&Wed
            [StartTime] => 08:00:00
            [EndTime] => 09:45:00
            [Location] => C12
        )
)

我希望按"Semester"分组,如下所示:

Array
(
    [Semester 1, 2015] => Array
        (
            [0] => Semester 1, 2015
            [1] => 11
            [2] => CSCI2203
            [3] => VISUAL PROGRAMMING
            [4] => NAME 0
            [5] => Tue&Thu
            [6] => 10:15:00
            [7] => 12:00:00
            [8] => C4
            [9] => Semester 1, 2015
            [10] => 12
            [11] => CSCI2903
            [12] => FUNDAMENTALS OF NETWORKING
            [13] => NAME 1
            [14] => Fri
            [15] => 10:15:00
            [16] => 12:00:00
            [17] => C13
            [18] => Semester 1, 2015
            [19] => 10
            [20] => CSCI2003
            [21] => DATA STRUCTURES
            [22] => NAME 2
            [23] => Mon&Wed
            [24] => 10:15:00
            [25] => 12:00:00
            [26] => C6
        )

    [Semester 2, 2015] => Array
        (
            [0] => Semester 2, 2015
            [1] => 5
            [2] => CSCI2903
            [3] => FUNDAMENTALS OF NETWORKING
            [4] => NAME 3
            [5] => Fri
            [6] => 08:00:00
            [7] => 09:45:00
            [8] => LH3
            [9] => Semester 2, 2015
            [10] => 3
            [11] => CSCI3003
            [12] => INTRODUCTION TO SOFTWARE ENGINEERING
            [13] => NAME 1
            [14] => Mon&Wed
            [15] => 08:00:00
            [16] => 09:45:00
            [17] => C12
        )
)

这是迄今为止我所拥有的:

$Semester = array();
$dataKey = array("Semester", "TimeTableID", "SubjectID", "SubjectName", "FacultyName", "Day", "StartTime", "EndTime", "Location");
for($i = 0; $i < count($Record); $i++)
{
    if(!in_array($Record[$i]['Semester'], $Semester))
    {
        $Semester[$Record[$i]['Semester']] = $Record[$i]['Semester'];
    }
    for($j = 0; $j < count($dataKey); $j++)
    {
        $Semester[$Record[$i]['Semester']] = $Record[$i][$dataKey[$j]];
    }
}
print_r($Semester);


$Record是我从数据库中检索的数据数组。但是这段代码只会给我最后一个值(location),如:

Array
(
    [Semester 1, 2015] => C6
    [Semester 2, 2015] => C12
)

2 个答案:

答案 0 :(得分:3)

您可以遍历数组并重新排列。我会将每个学期存储为一个数组数组。像这样:

$organized = array();

foreach( $resultsFromDB as $row )
{
    if( ! isset($organized[$row['Semester']]) ) {
        $organized[$row['Semester']] = array($row);
    }else {
        $organized[$row['Semester']][] = $row;
    }
}

print_r($organized);

您唯一需要担心的是学期字符串中的逗号(,)。这可能不适合作为数组的索引。在将学期设置为索引之前,您始终可以执行str_replace()

答案 1 :(得分:0)

您可以使用array_values将关联数组转换为索引数组。并且您可以使用array_merge来连接索引数组。

foreach ($Record as $r)
{
    $s = $r['Semester'];
    if(!in_array($s, $Semester))
    {
        // Get just the values without the keys
        $Semester[$s] = array_values($r);
    } else {
        // Concatenate this record to the sub-array 
        $Semester[$s] = array_merge($Semester[$s], array_values($r));
    }
}