PHP merge array + number and array_merge difference

Source: Internet
Author: User
This article mainly introduces the difference between PHP's array + number merging and array_merge. PHP's array fusion usually has two methods: one is to directly add the plus sign, the other is to use the array_merge function for addition. there is a difference between the two. if you need a friend, you can refer to the PHP array fusion. There are two methods. one is to directly use the plus sign for addition, the other is to use the array_merge function for addition. there is a difference between the two:

1. the addition indicates that the natural index in the array is not reset.
2. in the addition method, the values in the added array are not overwritten.
3. the natural index in the merge function will be reset.
4. the merge function is unrelated to the relationship between merge and merge. The value of the array parameter after the merge function overwrites the value of the same key as the previous array parameter.

Example:

Array 1:

$arr1 = array(  10 => 'valueof10',  11 => 'valueof11',  'key1' => 'dataofkey1',  'key2' => 'dataofkey2',  'array' => array(    'key1' => 1,    'key2' => 'abc',    'key3' => array(      1, 2, 'name' => 'peter'    ),  ),);

Array 2:

$arr2 = array(  10 => 'newvalueof10',  11 => 'newvalueof11',  'key1' => 'newdataofkey1',  'key3' => 'newdataofkey3',  'array' => array(    'key1' => 2,    'key2' => 'defg',    'key3' => array(      1, 2, 'name' => 'jonathan', 'gender' => 'male'    ),  ),);

Result comparison:

array (size=6) 10 => string 'valueof10' (length=9) 11 => string 'valueof11' (length=9) 'key1' => string 'dataofkey1' (length=10) 'key2' => string 'dataofkey2' (length=10) 'array' =>   array (size=3)   'key1' => int 1   'key2' => string 'abc' (length=3)   'key3' =>     array (size=3)     0 => int 1     1 => int 2     'name' => string 'peter' (length=5) 'key3' => string 'newdataofkey3' (length=13) array (size=8) 0 => string 'valueof10' (length=9) 1 => string 'valueof11' (length=9) 'key1' => string 'newdataofkey1' (length=13) 'key2' => string 'dataofkey2' (length=10) 'array' =>   array (size=3)   'key1' => int 2   'key2' => string 'defg' (length=4)   'key3' =>     array (size=4)     0 => int 1     1 => int 2     'name' => string 'jonathan' (length=8)     'gender' => string 'male' (length=4) 2 => string 'newvalueof10' (length=12) 3 => string 'newvalueof11' (length=12) 'key3' => string 'newdataofkey3' (length=13)

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.