Home > Backend Development > PHP Tutorial > PHP Merge Arrays and Append Arrays Example_PHP Tutorial

PHP Merge Arrays and Append Arrays Example_PHP Tutorial

WBOY
Release: 2016-07-13 10:30:23
Original
838 people have browsed it

We can use the array_merge() function to merge arrays in PHP. The array_merge() function returns a combined array. The resulting array starts with the first input array parameter and is appended in the order in which subsequent array parameters appear. Its form is:

 array array_merge (array array1 array2…,arrayN)

The following is an example of merging arrays in PHP:

 1

 2$fruits = array("apple","banana","pear");

 3$numbered = array("1","2","3");

 4$cards = array_merge($fruits, $numbered);

 5print_r($cards);

 6// Output result:

 7// Array ( [0] => apple [1] => banana [2] => pear [3] => 1 [4] => 2 [5] => 3 )

 8?>

Use PHP to append an array and use array_merge_recursive() to merge the two arrays together. Note that it is different from the array_merge() function. When the two arrays of array_merge() have duplicate items, they will be overwritten, while array_merge_recursive () will not. array_merge_recursive() syntax:

 array array_merge_recursive(array array1,array array2[…,array arrayN])

The following is an example of appending an array in PHP:

 1

 2$fruit1 = array("apple" => "red", "banana" => "yellow");

 3$fruit2 = array("pear" => "yellow", "apple" => "green");

 4$result = array_merge_recursive($fruit1, $fruit2);

 5print_r($result);

 6// Output result:

 7// Array ( [apple] => Array ( [0] => red [1] => green ) [banana] => yellow [pear] => yellow )

 8?>

Now apple points to an array, an indexed array consisting of two color values.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/765439.htmlTechArticlePHP Merge arrays We can use the array_merge() function, which returns a combined array. The resulting array starts with the first input array parameter, followed by the following array parameters...
source:php.cn
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