PHP Append Array

王林
Release: 2024-08-29 12:45:06
Original
393 people have browsed it

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.

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax of PHP Append Array

Below is the list of PHP array append and their syntax:

Syntax #1:

array_merge($array1, $array2);
Copy after login

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);
Copy after login

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);
Copy after login

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)
Copy after login

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);
?>
Copy after login

Output:

PHP Append Array

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);
?>
Copy after login

Output:

PHP Append Array

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);
?>
Copy after login

Output:

PHP Append Array

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);
?>
Copy after login

Output:

PHP Append Array

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);
?>
Copy after login

Output:

PHP Append Array

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);
?>
Copy after login

Output:

PHP Append Array

The above is the detailed content of PHP Append Array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source: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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!