note 84055 added to function.array-slice
| From: | disnider at gmail dot com | Date: | Wed, 25 Jun 2008 19:02:18 +0000 |
| Subject: | note 84055 added to function.array-slice | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-140817@lists.php.net to get a copy of this message | ||
it will split array into 2 halfs :
<?
function array_split($array)
{
$end=count($array);
$half = ($end % 2 )? ceil($end/2): $end/2;
return array(array_slice($array,0,$half),array_slice($array,$half));
}
//testing with odd count
$array=array('a','b','c','d');
echo "Odd: <br>";
var_dump(array_split($array));
//testing with even count
$array=array('a','b','c','d','f');
echo "Even: <br>";
var_dump(array_split($array));
?>
Output:
===========
Odd:
array(2) {
[0]=>
array(2) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
}
[1]=>
array(2) {
[0]=>
string(1) "c"
[1]=>
string(1) "d"
}
}
Even:
array(2) {
[0]=>
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
[1]=>
array(2) {
[0]=>
string(1) "d"
[1]=>
string(1) "f"
}
}
----
Server IP: 64.71.164.2
Probable Submitter: 92.113.103.34
----
Manual Page -- http://www.php.net/manual/en/function.array-slice.php
Edit -- https://master.php.net/note/edit/84055
Del: integrated -- https://master.php.net/note/delete/84055/integrated
Del: useless -- https://master.php.net/note/delete/84055/useless
Del: bad code -- https://master.php.net/note/delete/84055/bad+code
Del: spam -- https://master.php.net/note/delete/84055/spam
Del: non-english -- https://master.php.net/note/delete/84055/non-english
Del: in docs -- https://master.php.net/note/delete/84055/in+docs
Del: other reasons-- https://master.php.net/note/delete/84055
Reject -- https://master.php.net/note/reject/84055
Search -- https://master.php.net/manage/user-notes.php