根据PHP中的两个值对多维数组进行分组

编程入门 行业动态 更新时间:2024-10-28 12:25:17
本文介绍了根据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中的两个值对多维数组进行分组

本文发布于:2023-10-27 18:38:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1534205.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多维   数组   两个   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!