根据PHP中的相似值对数组进行分组 [英] Group arrays based on similar values in PHP

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

问题描述

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

I have an array with this structure:

<?php
$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' => 5,
            'teacher' => 'John',
        )
    );

我想根据某些属性对它们进行分组.例如,将学生人数相同的所有年级分组为:

where I want to group them based on certain attributes. For example to group all grades which have same number of students would be :

Grade 1 Class - Grade 3 Class     5

我像这样遍历数组:

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

foreach ($return as $key => $value) {
   echo implode(' - ', $value)."\t\t".$key;
}
// output is 
// Grade 1 Class - Grade 2 Class - Grade 3 Class      5

但是上面的代码也输出中间等级,如果我们可能不知道相似值出现在什么地方,我如何仅输出第一个和最后一个元素以获得所需的输出?

but the above code outputs the middle grade as well, how can I only output the first and last elements to get the desired output assuming we may not know where the similar values occurs?

例如,对于此数组,这是所需的输出:

For example, for this array this is the desired output:

Grade 1 Class - Grade 2 Class     5
Grade 3 Class - Grade 6 Class     8
Grade 7 Class                     9 
Grade 8 Class - Grade 9 Class     4 

推荐答案

如上所述,您只需要first和last元素.首先,您需要根据学生ID对数组进行分组,然后打印第一个和最后一个元素.我已将您的代码更新为-

As mentioned in question, you need only first and last element. First you need to group array according to student id and than print first and last element. I have update your code as -

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

$newArray = array();
$keyIndex = 0;
$s = null;
foreach ($grades as $key => $value) {
    if($s != $value['students']){
        $keyIndex++;
    }
    $newArray[$keyIndex][] = $value;
    $s = $value['students'];
}

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'] .' '. $lastGrade['students'];
    }
    else{
        echo $data[$gradeFirst]['title'] .' '. $data[$gradeFirst]['students'];
    }
    echo "<br>";
}

首先根据学生将所有元素分组.屏幕上显示第二循环打印输出.如果在第二个循环中添加了条件,以便检查学生是否有一个或多个年级.

First loop group all element according to student. Second Loop print output on screen. If condition is added in second loop if for check that there is single grade or multiple grade for a student.

返回输出为

Grade 1 Class - Grade 2 Class 5
Grade 3 Class - Grade 6 Class 8
Grade 7 Class 9
Grade 8 Class 4

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

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