我想根据年份和学期对这个多维数组进行排序

时间:2019-03-03 20:22:56

标签: php json

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [sem] => s2
                    [year] => 3
                    [subjectcode] => web
                    [subjectname] => webtechnology
                    [internal] => 30
                    [external] => 65
                    [credits] => 3
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [sem] => s2
                    [year] => 3
                    [subjectcode] => js654
                    [subjectname] => java
                    [internal] => 25
                    [external] => 60
                    [credits] => 3
                )

        )

)

1 个答案:

答案 0 :(得分:0)

[the second, ::slotted(*) rule]

首先创建一个$result = array(); foreach ($array as $array2) { foreach ($array2 as $values) { $result[$values['year']][$values['sem']][] = $values; } } 数组以将结果存储在其中。然后遍历两个嵌套数组以使用$result$values值到达最里面的year数组

当将sem数组添加到year数组时,每个sem数组的$values$values值都将用作键。这将按年份然后按学期对值进行排序。

结果:

$result