Extract a slice of the array

array:slice(ARRAY $array, INT $offset, INT $length=NULL, BOOL $preserve_keys=FALSE) : ARRAY

# get elements starting at index 1 (second position) to the end
array:slice(['a', 'b', 'c', 'd', 'e'], 1)
#=> [ "b", "c", "d", "e" ]

# get 2 starting at index 1 (second position)
array:slice(['a', 'b', 'c', 'd', 'e'], 1, 2)
#=> [ "b", "c" ]

array:slice(['a', 'b', 'c', 'd', 'e'], 2, 2)
#=> [ "c", "d" ]

# it works even if $length is larger than the array length
array:slice(['a', 'b', 'c', 'd', 'e'], 2, 100)
#=> [ "c", "d", "e" ]

# keep the array keys using the $preserve_keys = true
array:slice(['a', 'b', 'c', 'd', 'e'], 2, 100, true)
#=> [ 2 => "c", 3 => "d", 4 => "e" ]

# get the whole array
array:slice(['a', 'b', 'c', 'd', 'e'], 0)
#=> [ "a", "b", "c", "d", "e" ]

array:slice([1,2,3,4,5,6], 0, 1)
#=> [ 1 ]

# get all elements except the last two
array:slice([1,2,3,4,5,6], 0, -2)
#=> [ 1, 2, 3, 4 ]    

array:slice(['name' => 'foo', 'email' => 'bar'], 1, 1)
#=> [ "email" => "bar" ]

Copyright ©2013-2022 SunSed®. All rights reserved.