note 84055 deleted from function.array-slice by googleguy

From: Date: Tue, 22 Oct 2013 11:01:50 +0000
Subject: note 84055 deleted from function.array-slice by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-197034@lists.php.net to get a copy of this message
Note Submitter: disnider at gmail dot com ---- 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" } }

« previous php.notes (#197034) next »