Reputation:
I have a array contained 20 keys.
$arr = array(
"1" = "one",
"2" = "two",
.
.
.
"20" = "twenty"
);
Now I want to ignore ten key 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 should use 10 time of that function. something like this:
$arr = array_shift($arr); // ignoring first key
$arr = array_shift($arr); // ignoring second key
.
.
.
$arr = array_shift($arr); // ignoring tenth key
there is any better solution ?
Upvotes: 10
Views: 11930
Reputation:
It is little similar to you. However, You can also try. See the following example. It would be more comfortable code:
$students = array("one", "two", "three", "four", "five", "six", "more");
$to_be_shifted = 3;
for ($i = 1; $i <= $to_be_shifted; $i++) {
array_shift($students);
}
print_r($students);
Upvotes: 0
Reputation: 7261
I think you might be looking for array_splice
, which directly modifies the array (same as array_shift
), instead of returning a new array.
$n = 2; // number of elements to shift
array_splice($array, 0, $n);
Upvotes: 7
Reputation: 7785
Try this :
$array = array_slice($array, 10);
for more information, look here.
Upvotes: 13