• 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_pop() – Remove Last Element from Array

php array_pop() – Remove Last Element from Array

March 24, 2022 Leave a Comment

The php array_pop() function allows us to get and 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
)

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_pop() function gives us the ability to get and remove the last element of a given array.

To use array_pop(), just pass an 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
)

Getting the Last Element of an Array with array_pop()

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

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

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

echo array_pop($array);

// Output:
5

Removing the First Element from an Array with array_shift() in php

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

The array_shift() function does the opposite of array_pop() – it gets the firstelement and deletes it from the 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
)

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

Other Articles You'll Also Like:

  • 1.  php json_encode() Function – Get JSON Representation of Variables in php
  • 2.  php preg_match – Find Pattern in String Using Regex (Regular Expression)
  • 3.  php in_array – Check If Value is in php Array
  • 4.  php acosh – Find Hyperbolic Arccosine of Number Using acosh() Function
  • 5.  Check if String Ends with Given String in php with str_ends_with()
  • 6.  php Print Array – How to Print All Elements in Array
  • 7.  php sin – Find Sine of Number in Radians Using php sin() Function
  • 8.  php var_dump() Function – Print Information about Variable Type and Value
  • 9.  php Square Root with sqrt Function
  • 10.  php getimagesize – Get Height, Width and Image File Type

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