How to offset multiple elements from the beginning of an array

I have an array containing 20 keys.

$arr = array(
               "1" = "one",
               "2" = "two",
               .
               .
               .
               "20" = "twenty"
            );

Now I want to ignore ten keys first, and I want this output:

print_r($output);

// eleven, twelve, ..., twenty

here is one solution. using array_shift ($ arr) , but this solution is not optimized because I have to use 10 times for this function. something like that:

$arr = array_shift($arr); // ignoring first key
$arr = array_shift($arr); // ignoring second key
.
.
.
$arr = array_shift($arr); // ignoring tenth key

is there a better solution?

+4
source share
2 answers

Try the following:

$array = array_slice($array, 10);

for more information, see here .

+4
source

I think you can search array_splicethat directly modifies the array (the same as array_shift), instead of returning a new array.

$n = 2;  // number of elements to shift
array_splice($array, 0, $n);
0
source

All Articles