note 38668 deleted from function.array-chunk by aidan
| From: | aidan@php.net | Date: | Thu, 03 Jun 2004 10:16:18 +0000 |
| Subject: | note 38668 deleted from function.array-chunk by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-70421@lists.php.net to get a copy of this message | ||
Note Submitter: gphemsley@users.sourceforge.net
----
For those of you that need array_chunk() for PHP < 4.2.0, this function should do the trick:
<?php
function array_chunk( $input, $size, $preserve_keys = FALSE )
{
@reset( $input );
$i = $j = 0;
while( @list( , $value ) = @each( $input ) )
{
if( !( isset( $chunks[$i] ) ) )
{
$chunks[$i] = array();
}
if( count( $chunks[$i] ) < $size )
{
if( $preserve_keys )
{
$chunks[$i][$j] = $value;
$j++;
}
else
{
$chunks[$i][] = $value;
}
}
else
{
$i++;
if( $preserve_keys )
{
$chunks[$i][$j] = $value;
$j++;
}
else
{
$j = 0;
$chunks[$i][$j] = $value;
}
}
}
return $chunks;
}
?>