note 41347 added to function.array-chunk
| From: | mick at vandermostvanspijk dot nl | Date: | Wed, 07 Apr 2004 07:02:33 +0000 |
| Subject: | note 41347 added to function.array-chunk | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-67747@lists.php.net to get a copy of this message | ||
Above code needs a little correction, because the keys are not preserved, in the code beneath they
are.
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;
}
}
----
Manual Page -- http://www.php.net/manual/en/function.array-chunk.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+41347
Delete -- http://master.php.net/manage/user-notes.php?action=delete+41347&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+41347&report=yes
Search -- http://master.php.net/manage/user-notes.php