search for in the  
<array_change_key_casearray_combine>
Last updated: Thu, 19 May 2005

array_chunk

(PHP 4 >= 4.2.0, PHP 5)

array_chunk -- Split an array into chunks

Description

array array_chunk ( array input, int size [, bool preserve_keys] )

array_chunk() splits the array into several arrays with size values in them. You may also have an array with less values at the end. You get the arrays as members of a multidimensional array indexed with numbers starting from zero.

By setting the optional preserve_keys parameter to TRUE, you can force PHP to preserve the original keys from the input array. If you specify FALSE new number indices will be used in each resulting array with indices starting from zero. The default is FALSE.

Example 1. array_chunk() example

<?php
$input_array
= array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 2));
print_r(array_chunk($input_array, 2, true));
?>

The above example will output:

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
        )

    [1] => Array
        (
            [0] => c
            [1] => d
        )

    [2] => Array
        (
            [0] => e
        )

)
Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
        )

    [1] => Array
        (
            [2] => c
            [3] => d
        )

    [2] => Array
        (
            [4] => e
        )

)


User Contributed Notes
array_chunk
berndt at michael - berndt dot de
22-Apr-2005 10:22
SplitAssoziativArray() without array_chunk()
http://www.michael-berndt.de/ie/tux/assoziativen_array_zerteilen.htm
berndt at www dot michael-berndt dot de
10-Apr-2005 10:11
split array without array_chunk()
http://www.michael-berndt.de/ie/tux/split_array.htm
phpm at nreynolds dot me dot uk
17-Dec-2004 06:21
array_chunk() is helpful when constructing tables with a known number of columns but an unknown number of values, such as a calendar month. Example:

<?php

$values
= range(1, 31);
$rows = array_chunk($values, 7);

print
"<table>\n";
foreach (
$rows as $row) {
   print
"<tr>\n";
   foreach (
$row as $value) {
       print
"<td>" . $value . "</td>\n";
   }
   print
"</tr>\n";
}
print
"</table>\n";

?>

Outputs:

1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

The other direction is possible too, with the aid of a function included at the bottom of this note. Changing this line:
  $rows = array_chunk($values, 7);

To this:
  $rows = array_chunk_vertical($values, 7);

Produces a vertical calendar with seven columns:

1 6  11 16 21 26 31
2 7  12 17 22 27
3 8  13 18 23 28
4 9  14 19 24 29
5 10 15 20 25 30

You can also specify that $size refers to the number of rows, not columns:
  $rows = array_chunk_vertical($values, 7, false, false);

Producing this:

1 8  15 22 29
2 9  16 23 30
3 10 17 24 31
4 11 18 25
5 12 19 26
6 13 20 27
7 14 21 28

The function:

<?php

function array_chunk_vertical($input, $size, $preserve_keys = false, $size_is_horizontal = true)
{
  
$chunks = array();
  
   if (
$size_is_horizontal) {
      
$chunk_count = ceil(count($input) / $size);
   } else {
      
$chunk_count = $size;
   }
  
   for (
$chunk_index = 0; $chunk_index < $chunk_count; $chunk_index++) {
      
$chunks[] = array();
   }

  
$chunk_index = 0;
   foreach (
$input as $key => $value)
   {
       if (
$preserve_keys) {
          
$chunks[$chunk_index][$key] = $value;
       } else {
          
$chunks[$chunk_index][] = $value;
       }
      
       if (++
$chunk_index == $chunk_count) {
          
$chunk_index = 0;
       }
   }
  
   return
$chunks;
}

?>
mick at vandermostvanspijk dot nl
07-Apr-2004 05:02
[Editors note: This function was based on a previous function by gphemsley at nospam users dot sourceforge.net]

For those of you that need array_chunk() for PHP < 4.2.0, this function should do the trick:

<?php
if (!function_exists('array_chunk')) {
   function
array_chunk( $input, $size, $preserve_keys = false) {
       @
reset( $input );
      
      
$i = $j = 0;

       while( @list(
$key, $value ) = @each( $input ) ) {
           if( !( isset(
$chunks[$i] ) ) ) {
              
$chunks[$i] = array();
           }

           if(
count( $chunks[$i] ) < $size ) {
               if(
$preserve_keys ) {
                  
$chunks[$i][$key] = $value;
                  
$j++;
               } else {
                  
$chunks[$i][] = $value;
               }
           } else {
              
$i++;

               if(
$preserve_keys ) {
                  
$chunks[$i][$key] = $value;
                  
$j++;
               } else {
                  
$j = 0;
                  
$chunks[$i][$j] = $value;
               }
           }
       }

       return
$chunks;
   }
}
?>

<array_change_key_casearray_combine>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT