"red","b"=>"green","c"=>"blue"); echo array_shift($a); print_r"> "red","b"=>"green","c"=>"blue"); echo array_shift($a); print_r" />
Easy Tutorial
❮ Php Password_Verify Func Error Get Last ❯

PHP array_shift() Function

Complete PHP Array Reference Manual

Example

Remove the first element (red) from the array and return the removed element:

<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_shift($a);
print_r ($a);
?>

Definition and Usage

The array_shift() function is used to remove the first element from an array and return the removed element.

Note: If the keys are numeric, all elements will get new keys, starting from 0 and incrementing by 1 (see the example below).


Syntax

Parameter Description
array Required. Specifies the array.

Technical Details

Return Value: Returns the value of the removed element from the array, or NULL if the array is empty.
PHP Version: 4+
--- ---

More Examples

Example 1

Using numeric keys, all elements will get new keys:

<?php
$a=array(0=>"red",1=>"green",2=>"blue");
echo array_shift($a);
print_r ($a);
?>

If you want to preserve the numeric key indices, you can use the following method:

Example

<?php
$arr = array('1'=>1,'2'=>2,'3'=>'3');

function array_kshift(&$arr)
{
  list($k) = array_keys($arr);
  $r  = array($k=>$arr[$k]);
  unset($arr[$k]);
  return $r;
}
array_kshift($arr);
print_r($arr);
?>

The output of the above example will be:

Array
(
    [2] => 2
    [3] => 3
)

More information: PHP Remove the First Element of an Array

❮ Php Password_Verify Func Error Get Last ❯