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

The Programming Expert

Solving All of Your Programming Headaches

  • 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.  How to Reverse Array in php Without a Function or Other Variables
  • 2.  PHP Numerically Indexed Array Begin With Position 0
  • 3.  php max – Find Maximum Value of Array in php
  • 4.  php Print Array – How to Print All Elements in Array
  • 5.  php str_replace – Replace String in php
  • 6.  Replace Underscore with Space in php String
  • 7.  How to Create an Empty Array in php
  • 8.  Get Current Location Latitude Longitude in php
  • 9.  php e – Using M_E and exp() Function to Get Euler’s Constant e
  • 10.  php Random Number Generator with rand Function

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