note 41347 modified in function.array-chunk by tomsommer

From: Date: Sat, 07 Aug 2004 03:55:37 +0000
Subject: note 41347 modified in function.array-chunk by tomsommer
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-74126@lists.php.net to get a copy of this message
[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; } } ?> --was-- [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; } } ?> http://php.net/manual/en/function.array-chunk.php

« previous php.notes (#74126) next »