/** * Extract a slice of the array * * PHP Compat * * $length == null means up to end of array. Now, * sonots::array_slice($array, $offset, null, true) is possible. * <code> * array_slice($array, $offset, null (or 0 or -0)) returns array(), * </code> * thus, there was no way to slice up to end of array with preserving keys. * * @access public * @static * @param array $array The input array. * @param int $offset If offset is non-negative, the sequence will start at that offset in the array. * If offset is negative, the sequence will start that far from the end of the array. * @param mixed $length If length is given and is positive, then the sequence will have that many elements in it. * If length is given and is negative then the sequence will stop that many elements from the end of the array. * If it is omitted or NULL (add), then the sequence will have everything from offset up until the end of the array . * @param boolean $preserve_keys Note that array_slice() will reorder and reset the array indices by default. * You can change this behaviour by setting preserve_keys to true. * @return array * @version $Id: v 1.0 2008-06-07 11:14:46 sonots $ * @since v 1.7 */ function array_slice($array, $offset, $length = null, $preserve_keys = false) { if (is_null($length)) { if (!$preserve_keys) { return array_slice($array, $offset); } else { $keys = array_slice(array_keys($array), $offset); $ret = array(); foreach ($keys as $key) { $ret[$key] = $array[$key]; } return $ret; } } else { return php_compat_array_slice($array, $offset, $length, $preserve_keys); } }
function array_slice($array, $offset, $length = null, $preserve_keys = false) { return php_compat_array_slice($array, $offset, $length, $preserve_keys); }