Updated April 6, 2023
Introduction to PHP Append Array
Appending something to the existing one is one of the basic needs for any programming language. PHP itself has various built-in functions to handle the array append functionality. We can add two or more arrays to come up with a new (combination of all) array. The array is helpful when we have two different arrays and want to merge that into a single one for further processing. The array append could be pushing a new element to an array, adding one array to another array, merging 2 or more array together, etc. In array_merge() function return a new array after combing all the array passed within this array_merge() parameter.
Syntax of PHP Append Array
Below is the list of PHP array append and their syntax:
Syntax #1:
array_merge($array1, $array2);
Description – array_merge() is a PHP language built-in function. $array1 and $array2 are the two arrays we are looking to merge. It combined two single arrays into a single one.
Syntax #2:
array_push($array1, $array2);
Description – Again, array_push() is a PHP language built-in function. $array1 and $array2 are the two arrays we are looking to merge. In this process, the next array will appear in the next position of the first position. For example, if $array1 has the 5 elements, in this case, the complete $array2 will be placed on the 6th position.
Syntax #3:
array_push($array1, $val);
Description – array_merge() can be also used to adding the element to the array as well. $array1 is an array of whether $val is the value we want to add in the $array1. In this process, the $val will be added as normal value to the $array1 next coming position.
Elements of the single associate array can be performed using array merge. This key-value pared array will be converted into a single array. There are various other ways we can perform the array_merge() functionalities.
Syntax #4:
array_combine($array1, $array2)
array_combine() can be used to combine two single arrays into the array of associate (into key-value pared) array.
How does it Work?
For taking this array merge feature we need to have two arrays. Let’s say $array1 and $array2. We can merge these two into arrays to form a single array. This can be either done by writing our own custom code by using the PHP built-in functions. In PHP itself there are various ways we can achieve this as per our business requirements. On the other hand, the element(s) can be also added to the array.
Examples of PHP Append Array
Following are the examples as given below:
Example #1 – Merge two arrays to form a single one
In this example, we will have two arrays and we will try to merge that array using PHP array_merge() function.
Code:
<?php
$array1 = array(1, 2, 3, 4);
$array2 = array(4,5, 6);
$arr_merge = array_merge($array1, $array2);
print_r($arr_merge);
?>
Output:
Example #2 – Merge two array using array_push
Using array_push() function, the second one will be merge to the first one. In this function, the second array will be added to the next position of the first array. The complete array will be placed at the next place.
Code:
<?php
$array1 = array(1, 2, 3, 4);
$array2 = array(4,5, 6);
array_push($array1, $array2);
print_r($array1);
?>
Output:
We can see on the 4th position whole array has been placed.
Example #3 – Append a single element to an array
Code:
<?php
$array1 = array(1, 2, 3, 4);
array_push($array1, 2000);
print_r($array1);
?>
Output:
As we can see in the example, if we are adding a single element it will be added as a normal value at the next coming position of the array.
Example #4 – Feel the array by running loop
Code:
<?php
$testing = array();
for ($i=1; $i < 11 ; $i++) {
array_push($testing,$i);
}
print_r($testing);
?>
Output:
Example #5 – Merging associate array in PHP
Code:
<?php
$array1 = array("1" => "First");
$array2 = array("a2" => "Second", "a3" => "Third");
$result = array_merge($array1, $array2);
print_r($result);
?>
Output:
As we can see in this example, if we have the numeric key it will start from its traditional position. For the remaining, it will be added to the key-value.
Example #6 – Merging a single associate array in PHP
Code:
<?php
$array1 = array(1 => "Red", 3=>"Green", 2=>"Blue");
$result = array_merge($array1);
print_r($result);
?>
Output:
Recommended Articles
This is a guide to PHP Append Array. Here we also discuss the Introduction and how does php append array work along with different examples and its code implementation. You may also have a look at the following articles to learn more –