Two Methods for PHP to merge arrays and two methods for merging Arrays

Source: Internet
Author: User

Two Methods for PHP to merge arrays and two methods for merging Arrays

Preface

Which of the following statements is used to merge arrays before this operation?array_merge()This function, but recently I encountered an interview question about merging arrays during job change. What I was thinking at that time was to convert the two arrays into strings and then convert them into array output, the interviewer said that this idea is not quite correct. After bulabula finished talking about the basic array things, it was indeed because of experience issues, or because the code was too small to write, and I couldn't think of any other methods. Today I am Baidu, '+,

array_merge_recursive()It can also be used to merge arrays. Based on my memory, I will write the question to see 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 not to usearray_merge()The results are the same;

(array_merge()The merged array overwrites the key values of the associated arrays in the previous array, and the key values in the index format are merged in sequence)

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 result:

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 thatarray_merge_recursive()The function returns the same key value in the form of a new associated array and uses this key value as the key value of the two-dimensional array. Other indexes will not be affected.

Comparedarray_merge()If the array key is the same as the previous one, the previous value will not be overwritten.

2. Let's look at the combined array of '+:

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

Output result:

array (size=4) 'color' => string 'red' (length=3) 0 => int 5 1 => int 6 'type' => string 'fruit' (length=5)

From this result, we can see that using the '+' sign to merge the array is the first to overwrite the following, andarray_merge()On the contraryarray_merge()Even worse, if the content of the array appears as an index, it will be overwritten if the key value is the same after the merge!

Summary

Well, the above is all the content of this article. It is my personal summary. I am not very familiar with this article. If my summary is not in place, please criticize and correct it, be willing to accept it!

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.