note 53044 deleted from function.array-chunk by nlopess
| From: | nlopess@php.net | Date: | Sat, 24 Nov 2007 11:30:42 +0000 |
| Subject: | note 53044 deleted from function.array-chunk by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-132533@lists.php.net to get a copy of this message | ||
Note Submitter: cdblog at gmail dot com
----
<?php
/**
* @return array
* @author Cocol
* @desc use array_chunk with custom keys
* for more detail please visit http://php.clickz.cn/articles/array/array_chunk.html
*/
function array_chunk_custom($input_array = array(), $size = 0, $keys = array()) {
if (is_array($keys)) {
$values = array_values($input_array);
unset($input_array);
foreach ($keys as $key=>$val) {
$input_array[$val] = $values[$key];
}
}
$output_array = array_chunk($input_array,$size,1);
return $output_array;
}
$input_array = array('a', 'b', 'c', 'd',
'e','f','g','h');
$key_array =
array('i','ii','iii','iv','v','vi','vii');
print_r(array_chunk_custom($input_array,2,$key_array));
?>
output:
Array
(
[0] => Array
(
[i] => a
[ii] => b
)
[1] => Array
(
[iii] => c
[iv] => d
)
[2] => Array
(
[v] => e
[vi] => f
)
[3] => Array
(
[vii] => g
)
)