Adding and removing PHP array elements

Dani 1 Tallied Votes 130 Views Share

A PHP array is essentially a stack of elements that can be used to denote a list, represent a collection of variables, etc. You can easily use loops to iterate over each element of an array. As PHP is a loosely typed language, all of the elements of an array are not required to be of the same data type. You can also create an array of arrays, also known as a multidimensional array. Array keys can be either numerical or strings, and there can even be arrays with a combination of both key types. Arrays with string keys are known as an associative array, and they essentially function like an array of key-value pairs.

Here is an example of a numerical array:

Array
(
    [0] => foo
    [1] => bar
    [2] => baz
    [3] => bat
)

Here is an example of an associative array:

Array
(
    [banana] => yellow
    [apple] => red
    [grape] => green
)

Push elements onto an array

The easiest way to push multiple elements at a time onto the end of an array is to use PHP's native array_push() function as so:

$stack = array("orange", "banana");
array_push($stack, "apple", "raspberry");

When doing so, the elements added to the array will always have numerical keys, even if the existing array is an associative array. Something that people tend to not realize is that, when you just want to push one element onto the end of a PHP array, you should avoid the function call entirely, as so:

$stack = array("orange", "banana");
$stack[] = "apple";

This way there is no overhead when calling the function, making it significantly faster.

You can use this method for associative arrays as well:

$stack['key'] = "value";

Pop elements from an array

The opposite of array_push(), the native array_pop() function pops the last element off of the end of an array, shortening the array by one element.

It is used as so:

$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_pop($stack);

In this case, the value of $fruit is now "raspberry".

Shifting elements

If you think of an array like a big stack of items, you can use the array_shift() and array_unshift() functions to shift elements onto and off of the beginning of the stack.

To add elements to the head:

$queue = array("orange", "banana");
array_unshift($queue, "apple", "raspberry");

To remove elements from the head:

$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_shift($stack);

In this case, the value of $fruit is now "orange".

Note that these functions slow down the bigger the array is. This is because each element that remains in the array must be re-indexed (with numerical keys restarted at 0) each time either of them are used. It's obviously much more efficient to add or remove elements to the end of a big stack versus at the front of it.

Manipulating portions of an array

The array_slice() function can be used to extract a slice of the array as specified by an offset and length that are passed in as parameters. More information on how to use this function is here.

The array_splice() function works similarly in that it removes the elements designated by offset and length, and replaces them with something else. More information on how to use this function is here.

Other functions to manipulate arrays include array_replace() to replace elements of one array with those from another, array_merge() to combine multiple arrays into one, array_combine() to build an array by using an already existing array for its keys and another already existing array for its values, etc.

There are actually quite a handful of other array functions that make it quite useful to utilize arrays in PHP. Here is a comprehensive list.