search for in the  
<array_searcharray_slice>
Last updated: Thu, 19 May 2005

array_shift

(PHP 4, PHP 5)

array_shift --  Shift an element off the beginning of array

Description

mixed array_shift ( array &array )

array_shift() shifts the first value of the array off and returns it, shortening the array by one element and moving everything down. All numerical array keys will be modified to start counting from zero while literal keys won't be touched. If array is empty (or is not an array), NULL will be returned.

Note: This function will reset() the array pointer after use.

Example 1. array_shift() example

<?php
$stack
= array("orange", "banana", "apple", "raspberry");
$fruit = array_shift($stack);
print_r($stack);
?>

This would result in $stack having 3 elements left:

Array
(
   [0] => banana
   [1] => apple
   [2] => raspberry
)

and orange will be assigned to $fruit.

See also array_unshift(), array_push(), and array_pop().



User Contributed Notes
array_shift
arturo {dot} ronchi {at} gmail {dot} com
20-Apr-2005 08:24
Here is a little function if you would like to get the top element and rotate the array afterwards.

function array_rotate(&$arr)
{
  $elm = array_shift($arr);
  array_push($arr, $elm);
  return $elm;
}
09-Feb-2005 06:27
This function will save the key values of an array, and it will work in lower versions of PHP:

<?php

function array_shift2(&$array){
  
reset($array);
  
$key = key($array);
  
$removed = $array[$key];
   unset(
$array[$key]);
   return
$removed;
}

?>
James McGuigan
14-Dec-2004 01:26
while(array_shift()) can be used to process multiple arrays and/or database results in a single loop. The || short circuts and only evaluates the first statement until it runs out of data.

It can help to reduce duplicated code (the rule is code once and once only).

Note that each ($row = ) statement much be encased in ()'s otherwise you will get funny results. If you use two array_shift($array) statements and forget the ()'s, you will repeatedly get the first element of the first array for the for the count of the $array.

<?php

require_once('class.db.php');

$sql = "SELECT title FROM links";
$result = mysql_query($sql, $db->connection);

$defaults = array(
     array(
'title' => 'None'),
     array(
'title' => 'Unknown')
);

while ( (
$row = mysql_fetch_assoc($result))
     || (
$row = array_shift($defaults)))
{
  echo
$row['title'] . "<br>";
}

?>

This will print out (depending on database contents):
Title1
Title2
Title3
...
None
Unknown
Rich at home dot nl
15-Jan-2004 02:55
How about using foreach? That was made for looping through arrays after all.

<?php
  $styles
= array('style1', 'style2', 'style3', ... 'stylen');

  while (
true)  {
   foreach (
$styles as $style) {
     if (
haveMoreToPrint()) print('<div class="' . $style . '">Some content</div>');
     else break
2;
   }
  }
?>
mina86 at tlen dot pl
17-Oct-2003 10:30
archangel, 04-Mar-2003 08:11:
This method is just waste of CPU, RAM and everything. Better would be:

<?php
$styles
= array('style1', 'style2', 'style3', ... 'stylen');
$styles_count = count($styles);

for (
$i = 0; haveMoreToPrint(); $i = ($i+1)%$styles_count) {
   print(
'<div class="'.$styles[$i].'">Some content</div>');
}
?>

It does the same thing but faster :)
alex at netflex dot nl
13-Mar-2003 12:55
Hi,

if you want to shift the first element of a large array (more than 10.000?) and it must realy fast then you can use this better:

<?php
reset
($array);
list(
$oldKey, $oldElement) = each($array);
unset(
$array[$oldKey]);
?>

note: the index wil not be changed (not reindexed)
archangel at uro dot mine dot nu
04-Mar-2003 04:11
Here's a fun little trick for cycling a set of items in the case where you want to say loop thorugh a set of styles for a tiled effect:

<?php
$styles
= array('style1', 'style2', 'style3', ... 'stylen');

while (
haveMoreToPrint())
{
  
array_push($styles, array_shift($styles));

   print(
'<div class="'.$styles[0].'">Some content</div>');
}
?>

And since the elements that are being shifted off go onto the end, there is no concern as to how many or how few styles your have or how many times you are going to cycle through them.  No counters, no nothin.

<array_searcharray_slice>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT