PHPで配列をn分割する~array_chunk

2014-08-22

要素が10ある配列を、例えば3コづつに分割して再格納したいといったときの方法をど忘れしてましたので覚書です。利用用途的には、メニューを縦になが~く並べるのではなく、指定数になったら右にfloatさせるなどの時に便利かと思います。

配列の要素を3分割する

$array = array(
       'MENU 1'
      ,'MENU 2'
      ,'MENU 3'
      ,'MENU 4'
      ,'MENU 5'
      ,'MENU 6'
      ,'MENU 7'
      ,'MENU 8'
      ,'MENU 9'
    );

$chunk = array_chunk($array,3); //3コづつ分割

print_r($chunk);

結果

Array
(
    [0] => Array
        (
            [0] => MENU 1
            [1] => MENU 2
            [2] => MENU 3
        )

    [1] => Array
        (
            [0] => MENU 4
            [1] => MENU 5
            [2] => MENU 6
        )

    [2] => Array
        (
            [0] => MENU 7
            [1] => MENU 8
            [2] => MENU 9
        )

)

連想配列の要素を3分割する

$array = array(
    array(
         'category' => 'CATEGORY 1'
        ,'menu' => array(
           'MENU 1'
          ,'MENU 2'
          ,'MENU 3'
        )
    )
    ,array(
        'category' => 'CATEGORY 2'
        ,'menu' => array(
           'MENU 1'
          ,'MENU 2'
          ,'MENU 3'
          ,'MENU 4'
          ,'MENU 5'
        )
    )
    ,array(
        'category' => 'CATEGORY 3'
        ,'menu' => array(
           'MENU 1'
          ,'MENU 2'
          ,'MENU 3'
          ,'MENU 4'
          ,'MENU 5'
          ,'MENU 6'
          ,'MENU 7'
        )
    )
);

$new_Array = array();
foreach($array AS $array){
  $new_Array[] = array(
       'category' => $array['category']
      ,'menu' => array_chunk($array['menu'],3)//3つづつ分割
  );
}
print_r($new_Array);

結果

Array
(
    [0] => Array
        (
            [category] => CATEGORY 1
            [menu] => Array
                (
                    [0] => Array
                        (
                            [0] => MENU 1
                            [1] => MENU 2
                            [2] => MENU 3
                        )

                )

        )

    [1] => Array
        (
            [category] => CATEGORY 2
            [menu] => Array
                (
                    [0] => Array
                        (
                            [0] => MENU 1
                            [1] => MENU 2
                            [2] => MENU 3
                        )

                    [1] => Array
                        (
                            [0] => MENU 4
                            [1] => MENU 5
                        )

                )

        )

    [2] => Array
        (
            [category] => CATEGORY 3
            [menu] => Array
                (
                    [0] => Array
                        (
                            [0] => MENU 1
                            [1] => MENU 2
                            [2] => MENU 3
                        )

                    [1] => Array
                        (
                            [0] => MENU 4
                            [1] => MENU 5
                            [2] => MENU 6
                        )

                    [2] => Array
                        (
                            [0] => MENU 7
                        )

                )

        )

)