Delete the first element (red) from the array and return the deleted element:
<?php $a = array ( " a " => " red " , " b " => " green " , " c " => " blue " ) ; echo array_shift ( $a ) ; print_r ( $a ) ; ?>The array_shift() function is used to delete the first element in the array and return the deleted element.
Note: If the key is numeric, all elements will get new keys, starting at 0 and increasing by 1 (see example below).
array_shift( array )
parameter | describe |
---|---|
array | Required. Specifies an array. |
Return value: | Returns the value of the element removed from the array, or NULL if the array is empty. |
---|---|
PHP version: | 4+ |
Use numeric key names:
<?php$a=array(0=>"red",1=>"green",2=>"blue");echo array_shift($a);print_r ($a);?>