Existe-t-il une fonction PHP qui "pop" le premier élément du tableau?
array_pop()
apparaît dernier élément, mais je voudrais faire apparaître le premier.PHP - pop premier élément de tableau au lieu de dernier (array_pop reveresed)?
34
A
Répondre
67
Vous recherchez array_shift().
29
rapide Cheatsheet Si vous n'êtes pas familier avec le jargon, voici une traduction rapide des termes de remplacement, qui peut être plus facile à retenir:
* array_unshift() - (aka Prepend ;; InsertBefore ;; InsertAtBegin)
* array_shift() - (aka UnPrepend ;; RemoveBefore ;; RemoveFromBegin)
* array_push() - (aka Append ;; InsertAfter ;; InsertAtEnd)
* array_pop() - (aka UnAppend ;; RemoveAfter ;; RemoveFromEnd)
0
Pour moi array_slice() fonctionne très bien, Dis:
$arr = array(1,2,3,4,5,6);
//array_slice($array,offset); where $array is the array to be sliced, and offset is the number of array elements to be sliced
$arr2 = array_slice($arr,3);//or
$arr2 = array_slice($arr,-3);//or
$arr2 = array_slice($arr,-3,3);//return 4,5 and 6 in a new array
$arr2 = array_slice($arr,0,4);//returns 1,2,3 and 4 in a new array
+0
pourquoi ne pas j ust array_shift() c'est tellement plus efficace? –
grand un effet :) –