2016-07-16 113 views
0

我有一個包含具有8個值的數組的數組,我想將具有相同的teacher_name,school_namemonth_year的日期元素分組。日期元素將按照相同的教師姓名,學校和月份和年份組合的數組。由其他元素組成的數組元素

下面是整個陣列需要過濾:

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

您可以在陣列上使用的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 
       ) 
     ) 
)