PHP merge array append array example

Source: Internet
Author: User
Tags array example

PHP merge arrays we can use the array_merge () function. The array_merge () function returns a union array. The obtained array starts with the first input array parameter and is appended in the order in which the following array parameters appear. The format is:

Array array_merge (array array1 array2 ..., ArrayN)

The following is an example of PHP array merging:

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?>

Append an array with PHP and use array_merge_recursive () to merge the two arrays. Note that this function is different from the array_merge () function () the two arrays of will be overwritten when there are repeated items, while the 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 PHP append an array:

View sourceprint? 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?>

Apple points to an array consisting of two color values.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.