Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Select part of array

Tags:

arrays

php

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

like image 821
AllieCat Avatar asked Aug 26 '26 07:08

AllieCat


2 Answers

You want to use array_slice($array, 1, 5)

http://php.net/manual/en/function.array-slice.php

like image 150
Schleis Avatar answered Aug 28 '26 22:08

Schleis


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
)
like image 23
Amal Murali Avatar answered Aug 28 '26 22:08

Amal Murali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!