note 57124 deleted from function.array-chunk by nlopess
| From: | nlopess@php.net | Date: | Sat, 24 Sep 2005 19:19:11 +0000 |
| Subject: | note 57124 deleted from function.array-chunk by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-95722@lists.php.net to get a copy of this message | ||
Note Submitter:
----
<?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
)
)