基于PHP中的两个值对多维数组进行分组

时间:2016-11-26 06:31:11

标签: php arrays multidimensional-array

我有一个具有这种结构的数组:

$grades = array(
        'Grade 1'  => array(
            'title'   => 'Grade 1 Class',
            'students' => 5,
            'teacher' => 'Smith',
        ),
        'Grade 2' => array(
            'title'   => 'Grade 2 Class',
            'students' => 5,
            'teacher' => 'Smith',
        ),
        'Grade 3' => array(
            'title'   =>'Grade 3 Class',
            'students' => 8,
            'teacher' => 'John',
        ),
        'Grade 4' => array(
            'title'   =>'Grade 4 Class',
            'students' => 8,
            'teacher' => 'John',
        ),
        'Grade 5' => array(
            'title'   =>'Grade 5 Class',
            'students' => 8,
            'teacher' => 'John',
        ),
        'Grade 6' => array(
            'title'   =>'Grade 6 Class',
            'students' => 8,
            'teacher' => 'John',
        ),
        'Grade 7' => array(
            'title'   =>'Grade 7 Class',
            'students' => 9,
            'teacher' => 'John',
        ),
        'Grade 8' => array(
            'title'   =>'Grade 8 Class',
            'students' => 4,
            'teacher' => 'Tina',
        ),
       'Grade 9' => array(
            'title'   =>'Grade 9 Class',
            'students' => 4,
            'teacher' => 'Tina',
        )
    );

我想根据 学生和老师的数量对他们进行分组。

例如,上面的数组应输出:

Grade 1 Class - Grade 2 Class     5   Smith  // group 1
Grade 3 Class - Grade 6 Class     8   John   // group 2
Grade 7 Class                     9   John   // group 3
Grade 8 Class - Grade 9 Class     4   Tina   // group 4

所以要成为一组,他们必须有相同数量的学生和同一个老师。

以下仅根据学生人数分组(也有一些错误):

$newArray = array();
foreach ($grades as $key => $value) {
    $newArray[$value['students']][$key] = $value;
}

foreach ($newArray as $student => $data) {
    $gradeFirst =  key($data);
    // Check there are multiple grade or single grade
    if(count($data) > 1){
        $lastGrade = end($data);
        echo $data[$gradeFirst]['title'].' - '.$lastGrade['title'] .' '. $student;
    }
    else{
        echo $data[$gradeFirst]['title'].' '. $student;
    }
    echo "\n";
}

如何确保学生和教师都用于分组?

注意:顺序很重要,例如,如果第2类不同,则不会有第1类,第3类,第4类的组。在那种情况下,它将是:

Grade 1 Class                      // group 
Grade 2 Class                      // breaks the group 
Grade 3 Class - Grade 4 Class      // rest of the group

1 个答案:

答案 0 :(得分:2)

$newArray = [];
foreach($grades as $grade_key => $grade) {
    $group_key = $grade['teacher'].'_'.$grade['students'];
    if(!isset($newArray[$group_key]))
        $newArray[$group_key] = [];
    $newArray[$group_key][$grade_key] = $grade;
}
print_r($newArray); //just to check groupping is correct

我会像老师和学生一样将他们分组。

但是关于你输出格式的一些话......如果1级,3级和5级应该在一个组中,而2和4组在另一组中呢?你不能像 1年级 - 4年级那样输出它们,所以你应该选择更灵活的输出格式。

UPD,但是如果你想在问题更新时打破群组 - 试试这个:

$newArray = [];
$prev_group_key = '';
$group_key_appendix = 0;
foreach($grades as $grade_key => $grade) {
    $group_key = $grade['teacher'].'_'.$grade['students'];

    // perform group key unicalization in case tha groupping condition is the same but group should break
    if($group_key !== $prev_group_key) 
        $group_key_appendix++;
    $prev_group_key = $group_key;
    $real_group_key = $group_key.'-'.$group_key_appendix;

    if(!isset($newArray[$real_group_key]))
        $newArray[$real_group_key] = [];
    $newArray[$real_group_key][$grade_key] = $grade;
}
print_r($newArray);