How to use array shift in PHP

In this article, I will explain how the array_shift() function can be used in PHP.
  • 1767

array_shift() function in PHP

  • The array_shift() function is used to delete the first element from an array, and returns the value of the removed element.

  • In array_shift() function, If the keys are numeric, all elements will get new keys, starting from 0 and increases by 1.

  • In array_shift() function, All numerical array keys will be modified to start counting from zero.

Syntax

array_shift(array)

Parameter

  • array  array is required parameter. Specifies an array.

Example

The following example show to how the array_shift() function can be used in PHP.

<html>

<body>

<h3 style="color: green;">array_shift() function example in PHP</h3>  

    <?php 

    $list = array(0 => 'Student', 1=> 'Teacher', 2 => 'Advocate',3 => 'Engg.'); 

    $result= array_shift($list); 

    print_r($list); 

    echo '</br> Shift element : </br>'.$result; 

    ?>

</body>

</html>

 

Output

shift.jpg

You may also want to read these related articles here
 
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.