2016-07-16 13 views
0

配列に8個の値を含む配列があり、同じteacher_name,school_nameおよびmonth_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
あなたは配列をループにする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 
       ) 
     ) 
) 
関連する問題