Differences between PHP array addition and array_merge

Source: Internet
Author: User
: This article mainly introduces the differences between PHP array addition and array_merge. if you are interested in PHP Tutorial, please refer to it. PHP array processing has powerful functions that result in various data processing functions.

The array addition and array_merge let the author step on a small pitfall.

1. see demo1:

        public function action_test54(){                $a = array(                        '0'=>1567,                        '1'=>1568,                        '2'=>1569,                        '3'=>1570,                );                $b = array(                        '1'=>1571,                        '2'=>1572,                        '3'=>1573,                        '4'=>1574                );                d($a + $b);                dd(array_merge($a,$b));        }

Demo1 output:

array(5) (    0 => integer 1567    1 => integer 1568    2 => integer 1569    3 => integer 1570    4 => integer 1574)
array(8) (    0 => integer 1567    1 => integer 1568    2 => integer 1569    3 => integer 1570    4 => integer 1571    5 => integer 1572    6 => integer 1573    7 => integer 1574)

Conclusion:

$ A + $ B incremental coverage

Array_merge ($ a, $ B) merge

II. Demo2

        public function action_test54(){                $a = array(                        'a'=>1567,                        'b'=>1568,                        'c'=>1569,                        'd'=>1570,                );                $b = array(                        'b'=>1571,                        'c'=>1572,                        'd'=>1573,                        'e'=>1574                );                d($a + $b);                dd(array_merge($a,$b));        }

Demo2 output:

array(5) (    "a" => integer 1567    "b" => integer 1568    "c" => integer 1569    "d" => integer 1570    "e" => integer 1574)
array(5) (    "a" => integer 1567    "b" => integer 1571    "c" => integer 1572    "d" => integer 1573    "e" => integer 1574)

Conclusion:

$ A + $ B incremental coverage

Array_merger ($ a, $ B) $ B is replaced with $ a first.


The above introduces the differences between PHP array addition and array_merge, including the content, hope to be helpful to friends who are interested in PHP tutorials.

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.