由其他元素组合数组元素

时间:2016-07-16 05:52:07

标签: php

我有一个包含8个值的数组的数组,我想将具有相同teacher_nameschool_namemonth_year的日期元素分组。 date元素将是一个根据教师姓名,学校,月份和年份组合的数组。

以下是需要过滤的整个数组:

Array
(
    [0] => Array
        (
            [teacher_name] => CF
            [school_name] => School 1
            [month_year] => Jun 16
            [starttime] => 04:04 PM
            [endtime] => 05:04 PM
            [total_minutes] => 60
            [teacher_rate] => 20
            [date] => 09
        )

    [1] => Array
        (
            [teacher_name] => CF
            [school_name] => School 1
            [month_year] => Jun 16
            [starttime] => 04:04 PM
            [endtime] => 05:04 PM
            [total_minutes] => 60
            [teacher_rate] => 20
            [date] => 12
        )

    [2] => Array
        (
            [teacher_name] => CF
            [school_name] => School 1
            [month_year] => Oct 16
            [starttime] => 04:08 PM
            [endtime] => 05:08 PM
            [total_minutes] => 60
            [teacher_rate] => 20
            [date] => 01
        )

    [3] => Array
        (
            [teacher_name] => CF
            [school_name] => School 1
            [month_year] => Oct 16
            [starttime] => 04:08 PM
            [endtime] => 05:08 PM
            [total_minutes] => 60
            [teacher_rate] => 20
            [date] => 02
        )    

    [4] => Array
        (
            [teacher_name] => CF
            [school_name] => School 2
            [month_year] => Oct 16
            [starttime] => 04:08 PM
            [endtime] => 05:08 PM
            [total_minutes] => 60
            [teacher_rate] => 20
            [date] => 11
        )

    [5] => Array
        (
            [teacher_name] => CF
            [school_name] => School 2
            [month_year] => Oct 16
            [starttime] => 04:08 PM
            [endtime] => 05:08 PM
            [total_minutes] => 60
            [teacher_rate] => 20
            [date] => 22
        )
)   

我想要如下:

Array
(
    [0] => Array
        (
            [teacher_name] => CF
            [school_name] => School 1
            [month_year] => Jun 16
            [starttime] => 04:04 PM
            [endtime] => 05:04 PM
            [total_minutes] => 60
            [teacher_rate] => 20
            [date] => Array(09, 12) 
        )
    [1] => Array
        (
            [teacher_name] => CF
            [school_name] => School 1
            [month_year] => Oct 16
            [starttime] => 04:04 PM
            [endtime] => 05:04 PM
            [total_minutes] => 60
            [teacher_rate] => 20
            [date] => Array(01, 02)
        )
    [3] => Array
        (
            [teacher_name] => CF
            [school_name] => School 2
            [month_year] => Oct 16
            [starttime] => 04:04 PM
            [endtime] => 05:04 PM
            [total_minutes] => 60
            [teacher_rate] => 20
            [date] => Array(11, 22)
        )

)       

1 个答案:

答案 0 :(得分:1)

您可以使用foreach循环遍历数组并创建复合键以对数组项进行分组:

$data = [
    [
        'teacher_name' => 'A',
        'school_name' => 'School 1',
        'month_year' => 'Jun 16',
        'date' => '09'
    ],
    [
        'teacher_name' => 'B',
        'school_name' => 'School 2',
        'month_year' => 'Oct 16',
        'date' => '10'
    ],
    [
        'teacher_name' => 'A',
        'school_name' => 'School 1',
        'month_year' => 'Jun 16',
        'date' => '01'
    ],
    [
        'teacher_name' => 'B',
        'school_name' => 'School 2',
        'month_year' => 'Oct 16',
        'date' => '11'
    ]
];

$grouped = [];
foreach ($data as $item) {
    $key = md5(
        $item['teacher_name']
        . $item['school_name']
        . $item['month_year']
    );

    if (array_key_exists($key, $grouped)) {
        array_push($grouped[$key]['date'], $item['date']);
        continue;
    }

    $item['date'] = [$item['date']];
    $grouped[$key] = $item;
}

print_r($grouped);

输出:

Array
(
    [e2b1517951cf6670c37c9e906d58b97b] => Array
        (
            [teacher_name] => A
            [school_name] => School 1
            [month_year] => Jun 16
            [date] => Array
                (
                    [0] => 09
                    [1] => 01
                )
        )
    [a86fcb8ed3157949db35a340bc3f453d] => Array
        (
            [teacher_name] => B
            [school_name] => School 2
            [month_year] => Oct 16
            [date] => Array
                (
                    [0] => 10
                    [1] => 11
                )
        )
)