How to Add Elements to the End of an Array in PHP?
Last Updated :
08 Jul, 2024
In PHP, arrays are versatile data structures that can hold multiple values. Often, you may need to add elements to the end of an array, whether it's for building a list, appending data, or other purposes. PHP provides several ways to achieve this. In this article, we will explore different approaches to adding elements to the end of an array in PHP.
Approach 1: Using the array_push() Function
The array_push() function is a built-in PHP function that allows you to add one or more elements to the end of an array.
PHP
<?php
$arr = [10, 20, 30, 40];
// Add elements to the end of the array
array_push($arr, 50, 60);
print_r($arr);
?>
OutputArray
(
[0] => 10
[1] => 20
[2] => 30
[3] => 40
[4] => 50
[5] => 60
)
Explanation:
- $arr Array: An array containing some fruit names.
- array_push(): This function takes the array and the elements to be added as arguments. Here, 'grape' and 'mango' are added to the end of the $fruits array.
Approach 2: Using Square Brackets []
In PHP, you can also add elements to the end of an array by using square brackets [] without specifying an index.
PHP
<?php
$arr = [10, 20, 30, 40];
// Add elements to the
// end of the array
$arr[] = 50;
$arr[] = 60;
print_r($arr);
?>
OutputArray
(
[0] => 10
[1] => 20
[2] => 30
[3] => 40
[4] => 50
[5] => 60
)
Explanation:
- $arr Array: An array containing some numbers.
- Square Brackets []: By using [], we add the numbers 4 and 5 to the end of the $numbers array.
Approach 3: Using the += Operator
You can also use the += operator to append an associative array to the end of another associative array.
PHP
<?php
$person = ['fname' => 'Akash', 'lname' => 'Singh'];
// Add elements to the end of the array
$person += ['age' => 30, 'gender' => 'male'];
print_r($person);
?>
OutputArray
(
[fname] => Akash
[lname] => Singh
[age] => 30
[gender] => male
)
Explanation:
- $person Array: An associative array containing information about a person.
- += Operator: This operator is used to add the elements 'age' and 'gender' to the end of the $person array.
Approach 4: Using array_merge() Function
The array_merge() function merges one or more arrays, appending elements from subsequent arrays to the end of the first array.
Example:
PHP
<?php
$arr1 = [1, 2, 3];
$arr2 = [4, 5];
$res = array_merge($arr1, $arr2);
print_r($res);
?>
OutputArray
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Approach 5: Using array_splice() Function
The array_splice() function can be used to add elements to the end of an array by specifying the position at which to insert the elements and the number of elements to remove. By setting the position to the current length of the array and the number of elements to remove to 0, we can effectively append elements to the end.
Example:
PHP
<?php
// Sample array
$fruits = ["apple", "banana", "cherry"];
// Elements to add
$newFruits = ["grape", "mango"];
// Using array_splice() to add elements to the end of the array
array_splice($fruits, count($fruits), 0, $newFruits);
print_r($fruits);
?>
OutputArray
(
[0] => apple
[1] => banana
[2] => cherry
[3] => grape
[4] => mango
)
Similar Reads
How to find the index of an element in an array using PHP ? In this article, we will discuss how to find the index of an element in an array in PHP. Array indexing starts from 0 to n-1. Here we have some common approachesTable of ContentUsing array_search() FunctionUsing array_flip()Using a custom functionUsing a foreach LoopUsing array_keys FunctionUsing ar
6 min read
How to get elements in reverse order of an array in PHP ? An array is a collection of elements stored together. Every element in an array belong to a similar data type. The elements in the array are recognized by their index values. The elements can be subjected to a variety of operations, including reversal. There are various ways to reverse the elements
4 min read
How to Switch the First Element of an Arrays Sub Array in PHP? Given a 2D array where each element is an array itself, your task is to switch the first element of each sub-array with the first element of the last sub-array using PHP.Example:Input: num = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']];Output: [ ['g', 'b', 'c'], ['d', 'e', 'f'], ['a', 'h', '
2 min read
How to delete an Element From an Array in PHP ? To delete an element from an array means to remove a specific value or item from the array, shifting subsequent elements to the left to fill the gap. This operation adjusts the array's length accordingly, eliminating the specified element.This article discusses some of the most common methods used i
4 min read
How to Insert a New Element in an Array in PHP ? In PHP, an array is a type of data structure that allows us to store similar types of data under a single variable. The array is helpful to create a list of elements of similar types, which can be accessed using their index or key.We can insert an element or item in an array using the below function
5 min read