★ wanayoo — archive 1999 http://fr.php.net/manual/nl/function.array-shift.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<array_searcharray_slice>
view the version of this page
Last updated: Tue, 16 Nov 2004

array_shift

(PHP 4 , PHP 5)

array_shift --  Schuif een element van het begin van de array af

Beschrijving

mixed array_shift ( array array)

array_shift() schuift de eerste waarde van de array af en geeft deze terug, zodat de array een element korter wordt en alles een plaats wordt teruggeschoven. Alle numerieke array keys zullen worden gewijzigd zodat ze beginnen te tellen vanaf nul, terwijl string keys niet worden veranderd. Als array leeg (of geen array) is, zal NULL worden teruggegeven.

Voorbeeld 1. array_shift() voorbeeld

$stack = array ("orange", "banana", "apple", "raspberry");
$fruit = array_shift ($stack);

$stack heeft hierna nog 3 waarden:
Array
(
   [0] => banana
   [1] => apple
   [2] => raspberry
)
en $fruit zal de waarde orange hebben.

Zie ook array_unshift(), array_push() en array_pop().



add a note add a note User Contributed Notes
array_shift
Rich at home dot nl
15-Jan-2004 09: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
18-Oct-2003 05: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 07: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 11: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: Tue, 16 Nov 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2004 The PHP Group
All rights reserved.
This mirror generously provided by: nexen.net
Last updated: Sat Nov 27 05:15:21 2004 CET