Two ways to merge PHP arrays

Source: Internet
Author: User
This article mainly introduced the PHP array Fusion two methods, the interest friend's reference, hoped to be helpful to everybody.

There are two common approaches to array fusion in PHP, one is to add the plus sign directly, and the other is to add it using the Array_merge function, which is a bit different:

1. The natural index in the array will not be reset
2. In the Add method, the values in the added array are not overwritten
The natural index in the 3.merge function is reset
The 4.merge function, which does not matter by the merge and merge relationships, the more the following array parameter, its value, overrides the value of the same key as the previous array parameter

Example:

Array 1:

$arr 1 = Array (  valueof10 ', '  valueof11 ', ' key1 ' = '  dataofkey1 ',  ' key2 ' = ') Dataofkey2 ',  ' array ' = = Array (    ' key1 ' = 1,    ' key2 ' = ' abc ',    ' key3 ' = = Array (      1, 2, ') Name ' = ' Peter '),)  ;

Array 2:

$arr 2 = Array (  newvalueof10 ', '  newvalueof11 ', ' key1 ' = '  newdataofkey1 ',  ' key3 ' = > ' Newdataofkey3 ',  ' array ' = = Array (    ' key1 ' = 2,    ' key2 ' = ' defg ',    ' key3 ' = = Array (      1, 2, ' name ' = ' Jonathan ', ' gender ' = ' male '    ),)  ;

Results comparison:

Array (size=6), String ' Valueof10 ' (length=9) and string ' Valueof11 ' (length=9) ' key1 ' = = String ' Dataofke Y1 ' (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 ' = = Strin G ' 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 ' (le ngth=10) ' array ' = = Array (size=3) ' key1 ' + int 2 ' key2 ' = = String ' DEFG ' (length=4) ' Key3 ' = AR Ray (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) 

Summary: the above is the entire content of this article, I hope to be able to help you learn.

Related recommendations:

PHP read, edit, and save operations for files

PHP uses regular expressions to extract links from content

How PHP converts numbers into kanji

Related Article

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.