Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Add Elements to the End of an Array in PHP
Arrays are linear data structures used to handle data in programming. Sometimes while dealing with arrays we need to add new elements to an existing array. In this article, we will discuss several methods to add elements to the end of an array in PHP, complete with code examples and outputs.
Following are different approaches to adding elements to an array
Using Square Brackets []
The simplest way to add an element to the end of an array in PHP is by using square brackets []. This syntax is suitable when you want to add only a single element ?
$array[] = value;
Example
<?php $friends = ['Ayush', 'Antima']; $friends[] = 'Smrita'; // Adding a single element to the end print_r($friends); ?>
Array
(
[0] => Ayush
[1] => Antima
[2] => Smrita
)
Time Complexity: O(1)
Space Complexity: O(1)
Using array_push()
The array_push() function is used to add one or more elements to the end of an array. This method is mainly used when you need to add multiple items at once ?
array_push($array, $value1, $value2, ...)
Example
<?php $friends = ['Ayush', 'Antima']; array_push($friends, 'Smrita', 'Priti'); // Adding multiple elements print_r($friends); ?>
Array
(
[0] => Ayush
[1] => Antima
[2] => Smrita
[3] => Priti
)
Time Complexity: O(n), when multiple elements are added
Space Complexity: O(1)
Using array_merge()
If you want to combine two arrays, use the array_merge() method to merge multiple arrays into one. This method is useful when you want to add a whole array of new elements to your existing array ?
$array = array_merge($array1, $array2, ...);
Example
<?php $friends = ['Ayush', 'Antima']; $newFriends = ['Smrita', 'Priti']; $friends = array_merge($friends, $newFriends); print_r($friends); ?>
Array
(
[0] => Ayush
[1] => Antima
[2] => Smrita
[3] => Priti
)
Time Complexity: O(n)
Space Complexity: O(n)
Using the + Operator
You can also combine arrays using the + operator. This method works primarily with associative arrays and retains keys from the first array. If keys overlap, only the first array's values will remain ?
$array = $array1 + $array2;
Example
<?php $group1 = ['Ayush' => 1, 'Antima' => 2]; $group2 = ['Smrita' => 3, 'Priti' => 4]; $friends = $group1 + $group2; print_r($friends); ?>
Array
(
[Ayush] => 1
[Antima] => 2
[Smrita] => 3
[Priti] => 4
)
Time Complexity: O(n)
Space Complexity: O(1)
Using array_splice()
The array_splice() function is used for inserting, removing, or replacing elements in an array. You can use this method to insert new elements at any position, including the end ?
array_splice($array, $offset, $length, $replacement);
Example
<?php $friends = ['Ayush', 'Antima']; array_splice($friends, count($friends), 0, ['Smrita', 'Priti']); // Inserting at the end print_r($friends); ?>
Array
(
[0] => Ayush
[1] => Antima
[2] => Smrita
[3] => Priti
)
Time Complexity: O(n)
Space Complexity: O(n)
Conclusion
Use square brackets [] for single elements, array_push() for multiple elements, and array_merge() for combining arrays. Choose the method that best fits your specific use case and performance requirements.
