note 88833 added to function.array-slice
| From: | niels at madcap dot nl | Date: | Tue, 10 Feb 2009 13:41:29 +0000 |
| Subject: | note 88833 added to function.array-slice | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-150446@lists.php.net to get a copy of this message | ||
I've written this array slicer that remains key association and subdivides all data into
subarrays, I didn't profile this but I think it's faster then other implementations. By
default you will split it in half or you can get more arrays returned. This can be very useful when
you have an array structure of 47 items that needs to be divided into three (html) table columns.
/**
* Split an array into $n sub-arrays and remain key associations.
*
* @param $array
* The array structure which needs to be splitted.
* @param $n
* The number of sub-arrays the datastructure needs to
* be spliced into.
* @return
* An array containing $n sub-arrays.
*/
function array_smartsplit($array, $n = 2) {
$container = array_fill(0, $n, array());
$pointer = 0;
foreach ($array as $k => $v) {
$container[$pointer][$k] = $v;
$pointer++;
if ($pointer == $n) $pointer = 0;
}
return $container;
}
----
Server IP: 83.137.20.135
Probable Submitter: 82.95.23.27
----
Manual Page -- http://www.php.net/manual/en/function.array-slice.php
Edit -- https://master.php.net/note/edit/88833
Del: integrated -- https://master.php.net/note/delete/88833/integrated
Del: useless -- https://master.php.net/note/delete/88833/useless
Del: bad code -- https://master.php.net/note/delete/88833/bad+code
Del: spam -- https://master.php.net/note/delete/88833/spam
Del: non-english -- https://master.php.net/note/delete/88833/non-english
Del: in docs -- https://master.php.net/note/delete/88833/in+docs
Del: other reasons-- https://master.php.net/note/delete/88833
Reject -- https://master.php.net/note/reject/88833
Search -- https://master.php.net/manage/user-notes.php