PHP array_shift() Function

PHP array_shift() Function is used to remove the first element from an array and returned with shifted value at beginning . The array_shift() is a built-in function of PHP.

If the keys are numeric, all elements will get new keys and starting from 0 and increases by 1.

If index key is string, then all elements will remain same keys assigned in array.

Syntax :

array_shift($array) ;

$array : Required  It is type of array parameter.

Return :  It returns array with removed first element and shifted value. If array is empty, or is not an array, NULL will be returned.

Let's see below example to understand php array_shift() Function in details.

At below example, array_shift() function removes PHP value from $tutorials array and other values starts at 0 index because index key is numeric key.

 

Example :

<?php
echo "<br><br>Deleting first one element from an array and move other elements at beginning position.<br>";
$tutorials= array("PHP", "HTML", "JAVASCRIPT", "JAVA", "CSS");
array_shift($tutorials);
print_r($tutorials);

echo "<br><br>Deleting first one element from an array (Index key is string). All elements will remain same keys<br>";
$animals= array("a"=>"Dog", "b"=>"Cat", "c"=>"Cow", "d"=>"Fox", "e"=>"Lion");
array_shift($animals);
print_r($animals);
?>

Output :

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

64714