Home >Backend Development >PHP Tutorial >How to Add Elements to the End of an Array in PHP
Arrays are linear data structures used to process data in programming. Sometimes when we are processing arrays we need to add new elements to the existing array. In this article, we will discuss several ways to add elements to the end of an array in PHP, with code examples, output, and time and space complexity analysis for each method.
The following are different ways to add elements to an array:
[]
In PHP, the method to add elements to the end of an array is to use square brackets []
. This syntax only works in cases where we want to add only a single element. The following is the syntax:
<code class="language-php">$array[] = value;</code>
<code class="language-php"><?php $friends = ['Ayush', 'Antima']; $friends[] = 'Smrita'; // 向末尾添加单个元素 print_r($friends); ?></code>
<code>Array ( [0] => Ayush [1] => Antima [2] => Smrita )</code>
Time complexity: O(1)
Space complexity: O(1)
array_push()
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. The following is the syntax:
<code class="language-php">array_push($array, $value1, $value2, ...);</code>
<code class="language-php"><?php $friends = ['Ayush', 'Antima']; array_push($friends, 'Smrita', 'Priti'); // 添加多个元素 print_r($friends); ?></code>
The following is the output of the above code:
<code>Array ( [0] => Ayush [1] => Antima [2] => Smrita [3] => Priti )</code>
Time complexity: O(n), if multiple elements are added
Space complexity: O(1)
array_merge()
If you want to combine two arrays, you can use the array_merge()
method to combine multiple arrays into one. This method is useful when we want to add an entire new array of elements to an existing array. The following is the syntax:
<code class="language-php">$array = array_merge($array1, $array2, ...); </code>
<code class="language-php"><?php $friends = ['Ayush', 'Antima']; $newFriends = ['Smrita', 'Priti']; $friends = array_merge($friends, $newFriends); print_r($friends); ?></code>
The following is the output:
<code>Array ( [0] => Ayush [1] => Antima [2] => Smrita [3] => Priti )</code>
Time complexity: O(n)
Space complexity: O(n)
operator We can also combine arrays using the
operator. We should always remember that this method mainly applies to associative arrays and preserves the keys of the first array. If the keys overlap, only the value of the first array is preserved. The following is the syntax:
<code class="language-php">$array = $array1 + $array2;</code>
<code class="language-php"><?php $group1 = ['Ayush' => 1, 'Antima' => 2]; $group2 = ['Smrita' => 3, 'Priti' => 4]; $friends = $group1 + $group2; print_r($friends); ?></code>
The following is the output:
<code>Array ( [Ayush] => 1 [Antima] => 2 )</code>
Time complexity: O(n)
Space complexity: O(1)
array_splice()
array_splice()
Function is a very powerful and useful function. This function is used to insert, delete, or replace elements in an array. We can use this method to insert new elements anywhere (including the end). Here is the syntax of this method:
<code class="language-php">array_splice($array, $offset, $length, $replacement);</code>
<code class="language-php"><?php $friends = ['Ayush', 'Antima']; array_splice($friends, count($friends), 0, ['Smrita', 'Priti']); // 在末尾插入 print_r($friends); ?></code>
The following is the output:
<code>Array ( [0] => Ayush [1] => Antima [2] => Smrita [3] => Priti )</code>
Time complexity: O(n)
Space complexity: O(n)
The above is the detailed content of How to Add Elements to the End of an Array in PHP. For more information, please follow other related articles on the PHP Chinese website!