根据PHP中的两个值对多维数组进行分组 [英] Group multidimensional arrays based on two values in PHP

查看:69
本文介绍了根据PHP中的两个值对多维数组进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下结构的数组:

I have an array with this structure:

$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',
        )
    );

我想根据两者的学生和老师人数对他们进行分组.

And I want to group them based on both number of students and teacher.

例如,上面的数组应输出以下内容:

For example the above array should output this:

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

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

so to be in 1 group, they must have both same number of students and same teacher.

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

The following only groups based on number of students (it also has some bugs):

$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";
}

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

how can I make sure both student and teacher are used for grouping?

注意:顺序很重要,因此,例如,如果第2类不同,则第1类,第3类,第4类将没有分组.在这种情况下,它将是:

Note: the order is important, so for example, there won't be a group for class 1, class 3, class 4, if class 2 is different. In that case it would be :

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

推荐答案

$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

我会按照老师和学生的数量将它们分组.

I'd group them like this, by teacher and students count.

关于您输出格式的一些话...如果1、3和5年级应该在一组中,而2和4年级-在另一组中怎么办?您无法像 1年级-4年级那样输出它们,因此您应该选择更灵活的输出格式.

But some words about you output format... what if grades 1, 3 and 5 should be in one group, and 2 and 4 - in another? You cant output them like Grade 1 Class - Grade 4 Class, so you should choose more flexible output format.

UPD,但是如果您要像在有问题的更新中写的那样打破分组,请尝试以下操作:

UPD but if you want to break group as you wrote in question update - try this:

$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);

这篇关于根据PHP中的两个值对多维数组进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆