54

Suppose I have this array:

 $array = array('10', '20', '30.30', '40', '50');

Questions:

What is the fastest/easiest way to remove the first item from the above array?
What is the fastest/easiest way to remove the last item from the above array?

So the resulting array contains only these values:

  • '20'
  • '30.30'
  • '40'
6
  • 2
    why to you need "fastest" whay? how many items in your array? Commented Apr 20, 2010 at 13:05
  • @Col. Shrapnel: Consider a situation where there are more items in the array, he has just given a sample i think. Commented Apr 20, 2010 at 13:14
  • I refuse to consider such a nonsense. Large data arrays is database concern. Commented Apr 20, 2010 at 13:21
  • @Col. Shrapnel: You are right but you would see a host of such questions where no optimization is needed but people especially new comers still ask for that. Commented Apr 20, 2010 at 13:23
  • So, they to be educated of dangers of premature optimization. Commented Apr 20, 2010 at 13:26

7 Answers 7

81

Using array_slice is simplest

$newarray = array_slice($array, 1, -1);

If the input array has less than 3 elements in it, the output array will be empty.

Sign up to request clarification or add additional context in comments.

2 Comments

You've marked this as the correct answer, but if I were you I'd actually check to see if it truly is. This command necessitates copying a large section of memory. Using array_{shift,pop} will avoid copying that memory. Write a script where you instantiate a really large array and then call your function until the array has 0 (or 1) elements.
I benchmarked array_shift and array_pop solution vs array_slice solution. Using array_slice is very slower than the other one.
73

To remove the first element, use array_shift, to remove last element, use array_pop:

<?php    
$array = array('10', '20', '30.30', '40', '50');
array_shift($array);
array_pop($array);

1 Comment

I think your answer is the right one. It has better performance than array_slice.
11
array_pop($array); // remove the last element
array_shift($array); // remove the first element

Comments

7

array_slice is going to be the fastest since it's a single function call.

You use it like this: array_slice($input, 1, -1);

Make sure that the array has at least 2 items in it before doing this, though.

4 Comments

Wouldn't it be better to use -1 instead of count($input)-2 ?
Yeah, I didn't notice that at first. Fixed.
array_slice needs to create a new array, which means its way way way slower than array_pop
If the array has less than 2 items, array_slice will work.
6

Removes the first element from the array, and returns it:

array_shift($array);

Removes the last element from the array, and returns it:

array_pop($array);

If you dont mind doing them both at the same time, you can use:

array_shift($array,1,-1));

to knock off the first and last element at the same time.

Check the array_push, array_pop and array_slice documentation :)

1 Comment

array_shift does not accept 3 parameters. It has only one parameter. array_shift($array, 1, -1) is wrong!
6

Check this code:

$arry = array('10', '20', '30.30', '40', '50');
$fruit = array_shift($arry);
$fruit = array_pop($arry);
print_r($arry);

Comments

1
<?php
$array  = array("khan","jan","ban","man","le");
$sizeof_array = sizeof($array);
$last_itme = $sizeof_array-1;
//$slicearray= array_slice($array,'-'.$sizeof_array,4);// THIS WILL REMOVE LAST ITME OF ARRAY
$slicearray = array_slice($array,'-'.$last_itme);//THIS WILL REMOVE FIRST ITEM OF ARRAY
foreach($slicearray as $key=>$value)
{
  echo $value;  
  echo "<br>";
}   
?>

1 Comment

array_slice will create a new array, so it is very slow. This answer is using it twice!!