PHP » PYTHON |
login |
register |
about
|
PYTHON array_slice
is this article helpful?
|
Python replacement for PHP's array_slice
[
edit
| history
]
Use the slice syntax for sequences:
array[offset : offset+length] input = ["a", "b", "c", "d", "e"] output = input[2:] # returns ["c", "d", "e"] output = input[-2:-1] # returns ["d"] output = input[:3] # returns ["a", "b", "c"] More flexible function def array_slice( array , offset , length = None ): if is_array( array ) and not isinstance( array , dict ): if isinstance( array , set ): array = list( array ) return set( array[offset:length] ) return array[offset:length] return False array_slice(PHP 4, PHP 5) array_slice — Extract a slice of the array Description
array array_slice
( array $array
, int $offset
[, int $length
[, bool $preserve_keys= false
]] )
array_slice() returns the sequence of elements from the array array as specified by the offset and length parameters. Parameters
Return ValuesReturns the slice. Changelog
Examples
Example #1 array_slice() examples
<?php The above example will output: Array ( [0] => c [1] => d ) Array ( [2] => c [3] => d )
See Also
|
more
Recently updated
more
Most requested
more
Last requests
|