2 ways to merge arrays with PHP summary _php tips

Source: Internet
Author: User

Objective

I've been using this function since I used to merge the array, array_merge() but recently I encountered a merged array of face questions when I was changing my job, and I thought about converting two arrays into strings, merging and then converting to arrays, and the interviewer said the idea was not quite right, It's over. Bulabula told the base of the array of things, and then really because of experience, or code to write too little, think of any method, today I Baidu a bit, there is also a ' + ' number,

array_merge_recursive(), which can also be used to combine arrays, according to my memory, I write out the word and look at it:

$a = array (' Color ' => ' red ', 5,6); 
$b = Array (' Color ' => ' blue ', ' type ' => ' fruit ', 6,7); 
$arr = Array_merge ($a, $b); 
Var_dump ($arr);
Array (size=6)
 ' color ' => string ' Blue ' (length=4)
 0 => int 5
 1 => int 6
 ' type ' => string ' Fruit ' (length=5)
 2 => int 6
 3 => int 7

The requirement is to get the same effect in the condition of not being used array_merge() ;

(The array_merge() merged array overwrites the key values of the associative form array in the previous array, indexed in the order of the key values).

1. First use the array_merge_recursive () function to merge:

$a = array (' Color ' => ' red ', 5,6);
$b = Array (' Color ' => ' blue ', ' type ' => ' fruit ', 6,7);
$arr = array_merge_recursive ($a, $b);
Var_dump ($arr);

Output results:

Array (size=6)
 ' color ' => 
 Array (size=2)
  0 => string ' red ' (length=3)
  1 => string ' Blue ' (length =4)
 0 => int 5
 1 => int 6
 ' type ' => string ' fruit ' (length=5)
 2 => int 6
 3 => int 7

The result shows that the array_merge_recursive() function returns the value of the same key value as a new associative array, with the key value as the key value of the two-dimensional array, and the other indexed forms are unaffected.

Compared to the previous one will array_merge() not appear after the array key and the same as before the value of the previous one to overwrite the situation.

2. Then look at the "+" number of the merge array case:

$a = array (' Color ' => ' red ', 5,6);
$b = Array (' Color ' => ' blue ', ' type ' => ' fruit ', 6,7);
$arr = $a + $b;
Var_dump ($arr);

Output results:

Array (size=4)
 ' color ' => string ' red ' (length=3)
 0 => int 5
 1 => int 6
 ' type ' => string ' Frui T ' (length=5)

As you can see from this result, using the ' + ' number to merge the array, is the front cover behind, and the array_merge() opposite, and it is array_merge() more ruthless, if the contents of the array in the form of an index, if the value of the same key will be overwritten!

Summarize

Well, the above is the entire content of this article, is my personal summary, I entered the near future, the heat is poor, such as the summary is not in place please master criticism, must humbly accept!

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.