|
|
 |
array_shift (PHP 4 ) array_shift --
Dépile un élément au début d'un tableau
Descriptionmixed array_shift ( array array)
array_shift() extrait la première valeur d'un
tableau et la retourne, en raccourcissant le tableau d'un
élément, et en déplaçant tous les
éléments vers le bas.
Si array est vide, ou n'est pas un tableau,
array_shift() retourne NULL.
Note : This function will
reset() the array pointer after
use.
Exemple 1. Exemple avec array_shift() <?php
$stack = array ("orange", "banane", "pomme", "bleuet");
$fruit = array_shift ($stack);
print_r($stack);
?> |
$stack contient encore trois éléments :
Array
(
[0] => banane
[1] => pomme
[2] => bleuet
) |
et orange a été placé dans
$fruit.
|
Voir aussi
array_unshift(),
array_push() et
array_pop().
User Contributed Notes array_shift |
 |
RunMaster at gmx dot de
03-Sep-2001 01:32 |
|
Note that array_shift() is mixed and returns the REMOVED element, not the
shifted array!
A statement like
$array = array_shift(
array_flip( $array ) );
returns the first key of $array, not the
flipped and shifted $array.
You must use:
$array =
array_flip( $array );
array_shift( $array );
to let $array
contain the shifted array.
This applies to many functions in the
array context so look out for the returned types, before looking for bugs!
|
|
archangel at uro dot mine dot nu
04-Mar-2003 09: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:
$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.
|
|
alex at netflex dot nl
13-Mar-2003 05: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:
reset($array); list($oldKey, $oldElement) =
each($array); unset($array[$oldKey]);
note: the index wil not be
changed (not reindexed)
|
|
 |
| |