Home > Backend Development > PHP Tutorial > How to Add Elements to the End of an Array in PHP

How to Add Elements to the End of an Array in PHP

DDD
Release: 2025-02-07 11:17:17
Original
513 people have browsed it

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:

Use square brackets []

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:

$array[] = value;
Copy after login

Example

<?php 
$friends = ['Ayush', 'Antima'];
$friends[] = 'Smrita'; 
// 向末尾添加单个元素
print_r($friends);
?>
Copy after login

Output

<code>Array
(
    [0] => Ayush
    [1] => Antima
    [2] => Smrita
)</code>
Copy after login

Time complexity: O(1)

Space complexity: O(1)

Usage 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:

array_push($array, $value1, $value2, ...);
Copy after login

Example

<?php 
$friends = ['Ayush', 'Antima'];
array_push($friends, 'Smrita', 'Priti'); 
// 添加多个元素
print_r($friends);
?>
Copy after login

The following is the output of the above code:

<code>Array
(
    [0] => Ayush
    [1] => Antima
    [2] => Smrita
    [3] => Priti
)</code>
Copy after login
Copy after login

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

Space complexity: O(1)

Usage 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:

$array = array_merge($array1, $array2, ...); 
Copy after login

Example

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

The following is the output:

<code>Array
(
    [0] => Ayush
    [1] => Antima 
    [2] => Smrita
    [3] => Priti
)</code>
Copy after login

Time complexity: O(n)

Space complexity: O(n)

Use the 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:

$array = $array1 + $array2;
Copy after login

Example

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

The following is the output:

<code>Array
(
    [Ayush] => 1
    [Antima] => 2
)</code>
Copy after login

Time complexity: O(n)

Space complexity: O(1)

Usage 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:

array_splice($array, $offset, $length, $replacement);
Copy after login

Example

<?php 
$friends = ['Ayush', 'Antima'];
array_splice($friends, count($friends), 0, ['Smrita', 'Priti']); // 在末尾插入
print_r($friends);
?>
Copy after login

The following is the output:

<code>Array
(
    [0] => Ayush
    [1] => Antima
    [2] => Smrita
    [3] => Priti
)</code>
Copy after login
Copy after login

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!

Related labels:
php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template