How to Append Elements to an Array in PHP

Updated: January 10, 2024 By: Guest Contributor Post a comment

Overview

Understanding how to manipulate arrays is critical in PHP programming. Appending elements is one of the most common array operations, and PHP offers multiple ways to do it, enabling developers to add new values effortlessly.

Using the array_push() Function

The array_push() function is the most straightforward way to add one or more elements to the end of an array. Here’s a basic example:

<?php 
$array = array('apple', 'banana'); 
array_push($array, 'cherry', 'date'); 
print_r($array); 
?>

Output:

Array ( [0] => apple [1] => banana [2] => cherry [3] => date )

While array_push() is quite intuitive, it may not be the most efficient choice for appending a single element, as using the array’s square brackets is faster in that scenario.

Appending with Square Brackets

You can also append an element to an array by referencing the next available index using square brackets:

<?php 
$array = ['apple', 'banana']; 
$array[] = 'cherry'; 
print_r($array); 
?>

Output:

Array ( [0] => apple [1] => banana [2] => cherry )

This method is not only simpler but also offers better performance when adding individual elements.

Append Using the array_merge() Function

If you need to append another array to the existing one, array_merge() comes in handy:

<?php 
$array1 = ['apple', 'banana']; 
$array2 = ['cherry', 'date']; 
$mergedArray = array_merge($array1, $array2); 
print_r($mergedArray); 
?>

Output:

Array ( [0] => apple [1] => banana [2] => cherry [3] => date )

This function is more flexible as it allows you to combine multiple arrays into one.

Append on Array Creation with array()

You can append elements right at the time of array creation using the array() function. Here is how you can combine array creation with element appending:

<?php 
$array = array('apple', 'banana', 'cherry'); 
$newElement = 'date'; 
$array = array($newElement) + $array; 
print_r($array); 
?>

Output:

Array ( [0] => date [1] => apple [2] => banana [3] => cherry )

Note that the + operator does not reindex numeric array keys. If your array keys are meaningful and need to be preserved, this may be a significant advantage.

The Spread Operator in Array Elements

PHP 7.4 introduced the spread operator, which can be used to append elements from one array into another:

<?php 
$array1 = ['apple', 'banana']; 
$array2 = ['cherry', 'date']; 
$combinedArray = [...$array1, ...$array2]; 
print_r($combinedArray); 
?>

Output:

Array ( [0] => apple [1] => banana [2] => cherry [3] => date )

This modern approach is not only concise but also expressive, making your code tidier and more readable.

Custom Logic with array_splice()

The array_splice() function is typically used for removing elements, but it can also be employed for appending:

<?php 
$array = ['apple', 'banana']; 
$toInsert = ['cherry', 'date']; 
array_splice($array, count($array), 0, $toInsert); 
print_r($array); 
?>

Output:

Array ( [0] => apple [1] => banana [2] => cherry [3] => date )

In the example above, array_splice() allows precise control over where to insert the new elements.

Append and Maintain Key-Value Associations

Sometimes, you may have associative arrays where keys have a specific meaning. In such situations, maintaining the association is important:

<?php 
$array = ['a' => 'apple', 'b' => 'banana']; 
$array['c'] = 'cherry'; print_r($array); 
?>

Output:

Array ( [a] => apple [b] => banana [c] => cherry )

For associative arrays, the bracket notation allows you to specify both the key and the value for the element you’re appending.

Conclusion

Appending elements to an array in PHP is a versatile operation. Whether using functions like array_push() and array_merge(), or language constructs like the square brackets or spread operator, PHP provides efficient ways to grow arrays. These capabilities simplify the development process, allowing for clear, maintainable code.