note 70913 added to function.array-slice

From: Date: Fri, 03 Nov 2006 22:44:44 +0000
Subject: note 70913 added to function.array-slice
Groups: php.notes php.notes 
Request: Send a blank email to php-notes+get-119354@lists.php.net to get a copy of this message
The following function is the same as array_slice with preserve_keys=true, but it works with PHP versions < 5.0.2. When PHP >= 5.0.2 is available, the function uses the faster PHP-own array_slice-function with preserve_keys=true, otherwise it uses its own implementation. <?php /** * array_slice with preserve_keys for every php version * * @param array $array Input array * @param int $offset Start offset * @param int $length Length * @return array */ function array_slice_preserve_keys($array, $offset, $length = null) { // PHP >= 5.0.2 is able to do this itself if((int)str_replace('.', '', phpversion()) >= 502) return(array_slice($array, $offset, $length, true)); // prepare input variables $result = array(); $i = 0; if($offset < 0) $offset = count($array) + $offset; if($length > 0) $endOffset = $offset + $length; else if($length < 0) $endOffset = count($array) + $length; else $endOffset = count($array); // collect elements foreach($array as $key=>$value) { if($i >= $offset && $i < $endOffset) $result[$key] = $value; $i++; } // return return($result); } ?> Good for backwards compatibility I hope somebody might find this useful. ---- Server IP: 217.160.72.57 Probable Submitter: 80.228.219.209 ---- Manual Page -- http://www.php.net/manual/en/function.array-slice.php Edit -- https://master.php.net/note/edit/70913 Del: integrated -- https://master.php.net/note/delete/70913/integrated Del: useless -- https://master.php.net/note/delete/70913/useless Del: bad code -- https://master.php.net/note/delete/70913/bad+code Del: spam -- https://master.php.net/note/delete/70913/spam Del: non-english -- https://master.php.net/note/delete/70913/non-english Del: in docs -- https://master.php.net/note/delete/70913/in+docs Del: other reasons-- https://master.php.net/note/delete/70913 Reject -- https://master.php.net/note/reject/70913 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#119354) next »