I have an array:
$array = [1,2,3,4,5,6,7,8];
How do I select a subset of the array, to return just [2,3,4,5,6]?
I think it should be something along the lines of:
$array[2-6]
but that's not working
$array, you must use: array_slice($array, 1, 5, true) (cf. last parameter preserve_keys).Use array_slice():
$array = [1,2,3,4,5,6,7,8];
$output = array_slice($array, 1, 5);
print_r($output);
Output:
Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
[4] => 6
)
You can use array_slice http://www.php.net/manual/en/function.array-slice.php
array_slice($array, 1,5);
Use array_filter
function filter_arr($value) {
return 1 < $value && $value < 7;
}
array_filter($array, "filter_arr");
php array subset-- first result = 'array_slice — Extract a slice of the array'. It makes me wonder why asking on SO is preferred to Googling :([2-6]