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, outputs, and an analysis of the time and space complexity of each approach.

Following are different approaches to adding elements to an array ?

Using square brackets []

We add an element to the end of an array in PHP is by using square brackets []. This syntax is only suitable when we want to add only a single element. Following is the syntax ?

$array[] = value;

Example

<?php
   $friends = ['Ayush', 'Antima'];
   $friends[] = 'Smrita'; 
   // Adding a single element to the end
   print_r($friends);
?>

Output

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 we need to add multiple items at once. Following is the syntax ?

array_push($array, $value1, $value2, ...)

Example

<?php
   $friends = ['Ayush', 'Antima'];
   array_push($friends, 'Smrita', 'Priti'); 
   // Adding multiple elements
   print_r($friends);
?>

Following is the output of the above code ?

Array
(
 [0] => Ayush
 [1] => Antima
 [2] => Smrita
 [3] => Priti
)

Time Complexity: O(n), if multiple elements are added

Space Complexity: O(1)

Using array_merge()

If we want to combine two arrays, we use the array_merge() method to merge multiple arrays into one. This method is useful when we want to add a whole array of new elements to our existing array. Following is the syntax ?

$array = array_merge($array1, $array2, ...);

Example

<?php
   $friends = ['Ayush', 'Antima'];
   $newFriends = ['Smrita', 'Priti'];
   $friends = array_merge($friends, $newFriends);
   print_r($friends);
?>

Following is the output ?

Array
(
 [0] => Ayush
 [1] => Antima 
 [2] => Smrita
 [3] => Priti
)

Time Complexity: O(n)

Space Complexity: O(n)

Using the + Operator

We can also combine arrays using the + operator. We should always remember that this method works primarily with associative arrays and retains the keys from the first array. If keys overlap, then only the first array's values will remain left. Following is the syntax ?

$array = $array1 + $array2;

Example

<?php
   $group1 = ['Ayush' => 1, 'Antima' => 2];
   $group2 = ['Smrita' => 3, 'Priti' => 4];
   $friends = $group1 + $group2;
   print_r($friends);
?>

Following is the output ?

Array
(
 [Ayush] => 1
 [Antima] => 2
)

Time Complexity: O(n)

Space Complexity: O(1)

Using array_splice()

The array_splice() function is a very powerful and useful function. This function is used for inserting, removing, or replacing elements in an array. We can use this method to insert new elements at any position, including the end. Following is the syntax of this method ?

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);
?>

Following is the output ?

Array ( [0] => Ayush [1] => Antima [2] => Smrita [3] => Priti )

Time Complexity: O(n)

Space Complexity: O(n)

Updated on: 2024-11-01T13:15:36+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements