PHP Add To and Remove Elements From an Array

In this tutorial, I’ll show you eight different ways to add to, remove from and insert elements to/from an array in PHP. PHP array functions used include: array(), array_push(), array_unshift(), array_pop(), array_shift(), array_slice(), count(), array_diff() and array_splice().

Let’s start with our original array:

<?php
// Our original array
$array = array('one', 'two', 'three');

Here’s how you add elements to the end of a PHP array:

<?php
// Add elements to end of array
array_push($array, 'four', 'five');

Here’s how to elements to the beginning of a PHP array:

<?php
// Add elements to beginning of array
array_unshift($array, 'zero');

Here’s how to remove a single element from the end of an array:

<?php
// Remove one element from end of array
array_pop($array);

Here’s how to remove a single element from the beginning of an array:

<?php
// Remove one element from beginning of array
array_shift($array);

Here’s how to GET multiple elements from the end of an array:

<?php
// Get multiple elements from end of array
$lasttwo = array_slice($array, -2);

Here’s how to remove multiple elements from the end of an array and return what’s left:

<?php
// Remove multiple elements from end of array
$length = count($array);
$slice = array_slice($array, 0, $length-2);

Here’s how to remove specific elements (by value) from a PHP array:

<?php
// Remove specific elements from an array
$diff = array_diff($array, array('one', 'four'));

Here’s how to insert an element anywhere into an array:

<?php
// Insert element anywhere in array
array_splice( $array, 2, 0, 'inserted');

If you’d like to learn even more PHP, then consider taking my full PHP course here.

You might also like

Hack Culture Needs to Die

Anybody else tired of “hack” culture? “7 Hacks to Triple Your…” “A Secret Hack to Instantly Double Your…” “52 Little-Known Hacks to…” I asked my

Read More »
Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on reddit
Reddit
Share on pinterest
Pinterest
John Morris

JOHN MORRIS

I’m a 15-year veteran of freelance web development. I’ve worked with bestselling authors and average Joe’s next door. These days, I focus on helping other freelancers build their freelance business and their lifestyles.

The simple secrets to high-paying freelance clients

What makes clients willing to pay $5,000, $10,000 even $20,000 and up for your services? Download and install my mobile app and I’ll show you. It’s free. Just click the button below:

Clients Like:

Inc. Magazine Logo
Lewis Howes Logo
Ray Edwards Logo

WHAT OTHERS ARE SAYING

Xan Barksdale

Xan Barksdale

Very professional worker who is extremely knowledgable in WordPress and Wishlist Member. I would definitely hire again.

Thabo Motsoahae

John is one of the best instructors I have come across, I learned a lot from his online tutorials.

Chris Aitken

He significantly improved my site through his expert knowledge of PHP, CSS and Javascript. Would definitely recommend John to others.

Bradley Smith

John and I have worked together on numerous projects. John is very quick and efficient and was a pleasure to work with.

Andrew Malone

Andrew Malone

John Morris is exceptional in his ability to give focused insight into Freelancing and starting one’s business. His direct methods inspire confidence in his honesty.

Steve Dimmick

Steve Dimmick

John has provided expert knowledge and advice on multiple occasions that have helped me better serve my clients. John is a Rockstar!

Lori Grant

John did an outstanding job on my project. I highly recommend him and look forward to working with him on future projects.

Bob Patterson

Not only is John a very talented programmer and developer, he is also an excellent communicator. He has a talent for taking complex subjects and communicating them in terms that anyone can understand. This is a rare combination. This ability has enabled me to take my skills and knowledge to the next level. Thank you John for for all that you do.

Michael Skye

Michael Skye

John is a man of integrity, who gives generously of himself to projects and people he cares about.

Daniel Mohlendick

On the Freelancing on Upwork course: “This is by far the best course i have watched on Skillshare!! Thank you so much.”

Oliver Wainwright

Oliver Wainwright

I’m a fan. I have completed several of John’s courses. I find him very knowledgeable and he has a great delivery.

Jim DeJonge

Jim DeJonge

John has a relaxed and engaging manner. His advice is solid and the explanations are well thought out.

Lewis Howes

John is amazing at building membership sites. He converted one of my sites over from it’s existing (hardly working) platform over to the clean and simple to use WishList membership platform. I highly recommend using John and WishList for any of your membership site needs.