The Wayback Machine - https://web.archive.org/web/20240619183944/https://www.geeksforgeeks.org/php-append-one-array-to-another/
Open In App

PHP append one array to another

Last Updated : 03 Nov, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

Given two array arr1 and arr2 and the task is to append one array to another array.

Examples:

Input : arr1 = [ 1, 2 ]
        arr2 = [ 3, 4 ]

Output : arr1 = [ 1, 2, 3, 4 ]

Input : arr1 = [ "Geeks", "g4g" ]
        arr2 = [ "GeeksforGeeks" ]

Output : arr1 = [ "Geeks", "g4g", "GeeksforGeeks" ]

Using array_merge function: This function returns a new array after merging the two arrays.

Example:




<?php
$arr1 = array("Geeks", "g4g");
$arr2 = array("GeeksforGeeks", "Computer science portal");
  
// Get the merged array in the first array itself.
$arr1 = array_merge($arr1, $arr2); 
  
echo "arr1 Contents:";
  
// Use for each loop to print all the array elements.
foreach ($arr1 as $value) {
    echo $value . "\n";
}
?>


Output:

arr1 Contents:
Geeks
g4g
GeeksforGeeks
Computer science portal

Using array_push Method: This method pushes the second array element in the first array in-place.

Example:




<?php
$arr1 = array(1, 2);
$arr2 = array(3, 4);
  
// arr2 elements are being pushed in the arr1.
array_push($arr1 , ...$arr2); 
  
echo "arr1 = ";
  
// Use for each loop to print all the array elements.
foreach ($arr1 as $value) {
echo $value . ' ';
}
?>


Output:

arr1 = 1 2 3 4

Note: Another way to do it is by ‘ + ‘ but it gives fatal warning in the newer versions, hence it is not recommended.



Previous Article
Next Article

Similar Reads

Copy Elements of One Array into Another Array in PHP
Copying elements from one array to another is a common operation in PHP. Copy elements operation is used to create a duplicate, merge arrays, or extract specific elements. In this article, we will explore various approaches to copying elements from one array into another in PHP. Table of Content Using Assignment OperatorUsing foreach LoopUsing Spre
2 min read
How to copy a file from one directory to another using PHP ?
The copy() function in PHP is used to copy a file from source to target or destination directory. It makes a copy of the source file to the destination file and if the destination file already exists, it gets overwritten. The copy() function returns true on success and false on failure. Syntax: bool copy( string $source, string $destination, resour
2 min read
Insert data from one table to another table using PHP
In this article, we are going to insert data into another table from the existing table using PHP. Requirements: XAMPP Webserver PHP stands for Hypertext preprocessor. MySQL is a database query language for performing database operations. We are going to insert data from one table into another table by using MySQL Server. MySQL server is an open-so
2 min read
How to get the elements of one array which are not present in another array using JavaScript?
The task is to get the elements of one array that are not present in another array. Here we are going to use JavaScript to achieve the goal. Here are a few techniques discussed. Approach Take the arrays in variables.Use the .filter() method on the first array and check if the elements of the first array are not present in the second array, Include
2 min read
How to move an array element from one array position to another in JavaScript?
In JavaScript, we can access an array element as in other programming languages like C, C++, Java, etc. Also, there is a method called splice() in JavaScript, by which an array can be removed or replaced by another element for an index. So to move an array element from one array position to another we can splice() method or we can simply use array
4 min read
ArrayObject append() function in PHP
The append() function of the ArrayObject class in PHP is used to append a given value onto an ArrayObject. The value being appended can be a single value or an array itself. Syntax: void append($value) Parameters: This function accepts a single parameter $value, representing the value to be appended. Return Value: This function does not returns any
1 min read
PHP | ArrayIterator append() Function
The ArrayIterator::append() function is an inbuilt function in PHP which is used to append an element into an array iterator. Syntax: void ArrayIterator::append( mixed $value ) Parameters: This function accepts single parameter $value that holds the value that need to be append. Return Value: This function does not return any value. Below programs
1 min read
PHP | AppendIterator append() Function
The AppendIterator::append() function is an inbuilt function in PHP which is used to append an iterator. Syntax: void AppendIterator::append( Iterator $iterator ) Parameters: This function accepts single parameter $iterator which holds the iterator to append. Return Value: This function does not return any value. Below programs illustrate the Appen
1 min read
How to append data in JSON file through HTML form using PHP ?
The purpose of this article is to append data to a JSON file through HTML form using PHP. Approach 1: If the JSON file is not created then we create a new JSON file, send data to it, and append data in it. To see how to create a JSON file by taking data from the HTML form, refer this link. Approach 2: If the JSON file is already created then we dir
4 min read
How to append a string in PHP ?
We have given two strings and the task is to append a string str1 with another string str2 in PHP. There is no specific function to append a string in PHP. In order to do this task, we have the this operator in PHP: Table of Content Using Concatenation assignment operator (".=")Using Concatenation Operator(".")Using Double Quotes with Variables (In
2 min read