cvs: pear /PHP_Compat/Compat/Function array_chunk.php
| From: | Aidan Lister | Date: | Sat, 12 Jun 2004 07:04:22 +0000 |
| Subject: | cvs: pear /PHP_Compat/Compat/Function array_chunk.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-20545@lists.php.net to get a copy of this message | ||
aidan Sat Jun 12 03:04:22 2004 EDT
Modified files:
/pear/PHP_Compat/Compat/Function array_chunk.php
Log:
Fixed bugs:
- Size may be any numeric value including string('3.1')
- Failed to create more than 2 chunks
Thanks to Thiemo Mättig <thiemo.maettig@gmx.de> for the patch
http://cvs.php.net/diff.php/pear/PHP_Compat/Compat/Function/array_chunk.php?r1=1.6&r2=1.7&ty=u
Index: pear/PHP_Compat/Compat/Function/array_chunk.php
diff -u pear/PHP_Compat/Compat/Function/array_chunk.php:1.6
pear/PHP_Compat/Compat/Function/array_chunk.php:1.7
--- pear/PHP_Compat/Compat/Function/array_chunk.php:1.6 Sat Jun 12 02:53:00 2004
+++ pear/PHP_Compat/Compat/Function/array_chunk.php Sat Jun 12 03:04:22 2004
@@ -16,7 +16,7 @@
// | Authors: Aidan Lister <aidan@php.net> |
// +----------------------------------------------------------------------+
//
-// $Id: array_chunk.php,v 1.6 2004/06/12 06:53:00 aidan Exp $
+// $Id: array_chunk.php,v 1.7 2004/06/12 07:04:22 aidan Exp $
//
@@ -27,7 +27,7 @@
* @package PHP_Compat
* @link http://php.net/function.array_chunk
* @author Aidan Lister <aidan@php.net>
- * @version $Revision: 1.6 $
+ * @version $Revision: 1.7 $
* @since PHP 4.2.0
*/
if (!function_exists('array_chunk'))
@@ -39,58 +39,33 @@
return null;
}
- if (!is_int($size)) {
+ if (!is_numeric($size)) {
trigger_error('array_chunk() expects parameter 2 to be long, ' .
gettype($size) . ' given', E_USER_WARNING);
return null;
}
- $chunks = array ();
+ $size = (int)$size;
+ if ($size <= 0)
+ {
+ trigger_error('array_chunk() Size parameter expected to be greater than 0',
E_USER_WARNING);
+ return null;
+ }
- // The input is smaller than the chunksize
- if (count($input) <= $size)
- {
- if ($preserve_keys !== false)
- {
- foreach ($input as $key => $value)
- {
- $chunks[0][$key] = $value;
- }
- }
- else
- {
- foreach ($input as $value)
- {
- $chunks[0][] = $value;
- }
- }
- }
-
- // The input is larger than the chunksize
- else
- {
- $j = $i = 0;
+ $chunks = array();
+ $i = 0;
- if ($preserve_keys !== false)
- {
- foreach ($input as $key => $value)
- {
- $chunks[$j][$key] = $value;
- if (++$i == $size) {
- $j++;
- }
- }
- }
- else
- {
- foreach ($input as $key => $value)
- {
- $chunks[$j][] = $value;
- if (++$i == $size) {
- $j++;
- }
- }
- }
- }
+ if ($preserve_keys !== false)
+ {
+ foreach ($input as $key => $value) {
+ $chunks[(int)($i++ / $size)][$key] = $value;
+ }
+ }
+ else
+ {
+ foreach ($input as $value) {
+ $chunks[(int)($i++ / $size)][] = $value;
+ }
+ }
return $chunks;
}