基于PHP中类似值的组数组

时间:2016-11-26 01:31:15

标签: php arrays

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

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

我想根据某些属性对它们进行分组。例如,对具有相同学生人数的所有成绩进行分组:

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

但上面的代码也输出了中间等级,我怎么能输出第一个和最后一个元素来得到所需的输出,假设我们可能不知道相似值出现在哪里?

例如,对于this array,这是所需的输出:

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 

2 个答案:

答案 0 :(得分:0)

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

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

第一个循环组根据学生的所有元素。屏幕上的第二循环打印输出。如果要检查学生是否有单一年级或多年级,则在第二个循环中添加条件。

将输出返回为

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

答案 1 :(得分:0)

这对我有用

<?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' => 8,
            'teacher' => 'John',
        ),
        'Grade 4' => array(
            'title'   =>'Grade 4 Class',
            'students' => 5,
            'teacher' => 'Tina',
        )
    );


foreach ( $grades as $outer_k => $outer_v ) {
    // hold current $outer_k and start looping through $grades agaain, doing
    // comparison
    foreach ( $grades as $inner_k => $inner_v ) {
        // check inner_k is not comparing itself to itself
        if ( $inner_k !== $outer_k ) {
            if ( $inner_v['students'] === $outer_v['students'] ) {
                $matches[] = $inner_v['title'];
                $num_students = $inner_v['students'];
            }
        }
    }
}

sort($matches);
$result = array_unique($matches);
$result = array_values($result);

$length = count($result) - 1;
echo $result[0] . " - " . $result[$length] . "\t\t{$num_students}\n";

生成此输出:

Grade 1 Class - Grade 4 Class           5