• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / PHP / php array_shift() – Remove the First Element from Array

php array_shift() – Remove the First Element from Array

March 24, 2022 Leave a Comment

The php array_shift() function allows us to get and delete the first element of an array in php.

$array = array(1, 2, 3, 4, 5);

array_shift($array);

print_r($array);

// Output:
Array
(
    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 5
)

When working with arrays and collections of data in php, it is useful to be able to add or remove items from our data structures easily.

The array_shift() function gives us the ability to remove the first element of a given array.

To use array_shift(), just pass an array.

Below is an example of how you can use array_shift() to delete the first element of an array in php.

$array = array(1, 2, 3, 4, 5);

array_shift($array);

print_r($array);

// Output:
Array
(
    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 5
)

Getting the First Element of an Array with array_shift()

The array_shift() function returns the first element of an array. So with array_shift(), we can get the first element of an array.

Below is a simple example in php of how to get the first element of an array using array_shift().

$array = array(1, 2, 3, 4, 5);

echo array_shift($array);

// Output:
1

Getting the Last Element of an Array with array_pop()

If you’d instead like to get the last element of an array, you can use the php array_pop() function.

The array_pop() function does the opposite of array_shift() – it gets the last element and deletes it from the array.

Below is an example of how you can use array_pop() to delete the last element of an array in php.

$array = array(1, 2, 3, 4, 5);

array_pop($array);

print_r($array);

// Output:
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

Hopefully this article has been useful for you to learn how to use the php array_shift() function.

Other Articles You'll Also Like:

  • 1.  Reversing an Array in php with array_reverse() Function
  • 2.  Using strtoupper() in php to Convert String to Uppercase
  • 3.  php array_map() – Create New Array from Callback Function
  • 4.  Replace Underscore with Space in php String
  • 5.  Check if String Starts with Given String in php with str_starts_with()
  • 6.  Get Current Month of Date in php
  • 7.  Delete Cookie Using php
  • 8.  PHP random_int() Function – Generate Random Integers in PHP
  • 9.  Capitalize First Letter of String with php ucfirst() Function
  • 10.  How to Get Current System IP Address in php

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy