PHP - Adding and Removing Array Elements

Introduction

You already know that you can add elements to an array using square bracket syntax.

For example:

$myArray[] ="new value";
$myArray["newKey"] ="new value";

This syntax is fine for simple scenarios.

PHP has five useful functions that you can use to add and remove elements:

Function Description
array_unshift() Adds one or more new elements to the start of an array
array_shift() Removes the first element from the start of an array
array_push() Adds one or more new elements to the end of an array
array_pop() Removes the last element from the end of an array
array_splice() Removes element(s) from and/or adds element(s) to any point in an array

Related Topics