note 57124 added to function.array-chunk
| From: | php-general at lists dot php dot net | Date: | Sat, 24 Sep 2005 15:48:55 +0000 |
| Subject: | note 57124 added to function.array-chunk | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-95710@lists.php.net to get a copy of this message | ||
<?php
/**
* @source http://www.ipoll.cn/?op=ViewArticle&articleId=365&blogId=1
* @blog http://www.ipoll.cn/
* @author freish chilly
* @email ipoller at gmail dot net (change "net" to "com")
* @desc the replacement of array_chunk for old versions(<4.2.0)
* @param size
* @param preserve_keys 1->preserve the original keys 0->new number indices will be used in
each resulting array with indices starting from zero
**/
function custom_array_chunk ( $array = array() , $size = 2 , $preserve_keys = 0) {
$array_length = count($array);
$i = 0;
$j = 0;
$k = 0;
foreach( $array as $key => $val ) {
if ( $i < $size ) {
if ( $preserve_keys == 1) {
$array_chunked[$j][$key] = $val;
} else {
$array_chunked[$j][$k] = $val;
$k++;
}
$i++;
} else {
$i = 0;
$j++;
}
}
return $array_chunked;
}
$input_array = array('ad'=>'a', 'b', 'c', 'd',
'e');
print_r(custom_array_chunk($input_array, 2));
print_r(custom_array_chunk($input_array, 2, true));
?>
Content-type: text/html
X-Powered-By: PHP/4.3.10
Array
(
[0] => Array
(
[0] => a
[1] => b
)
[1] => Array
(
[2] => d
[3] => e
)
)
Array
(
[0] => Array
(
[ad] => a
[0] => b
)
[1] => Array
(
[2] => d
[3] => e
)
)
----
Manual Page -- http://www.php.net/manual/en/function.array-chunk.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+57124
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57124&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57124&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57124&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57124&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57124&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57124&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57124&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+57124&report=yes
Search -- http://master.php.net/manage/user-notes.php