Php array merge

Source: Internet
Author: User
: This article mainly introduces php array merging. if you are interested in the PHP Tutorial, you can refer to it. There is a big gap between php array and java. Although the weak design of php may be criticized by many java and C ++ programmers, however, I think php is far behind java and C ++ in the design of array containers.

In addition to the incomplete design of java containers, java allows the existence of arrays of class C ++, which is the only java language that does not satisfy the existence of all objects.

In fact, the existence of containers must reduce the efficiency. However, if you emphasize the efficiency too much, why not simply use the machine code? So I don't like those ideas that take the php container design too far.

As we all know, php's array itself is map.

For an ordinary array, its internal structure is:

$arr1=array(1,2,3,4,5);print_r($arr1,1); Array(    [0] => 1    [1] => 2    [2] => 3    [3] => 4    [4] => 5)

For the map we generally understand, php implements this internally:

$arr2=array('c'=>1,'y'=>2,'m'=>3);print_r($arr2,1); Array(    [c] => 1    [y] => 2    [m] => 3)

For array merging, you can think that php uses the overload function similar to C ++ and reloads the plus sign:

print_r($arr1+$arr2,1);Array(    [0] => 1    [1] => 2    [2] => 3    [3] => 4    [4] => 5    [c] => 1    [y] => 2    [m] => 3)

This sort of array merge makes it easy for you to traverse the array from the beginning, because in many cases, you do not use the array in random access mode, but instead traverse the array from the beginning to the end.

However, here, one problem that you can easily ignore is that the essence of array is map. If the two arrays have the same key, they are easily ignored:

$arr1=array(1,2,3,4,5);$arr2=array(7,8,9);$arr3=array('1'=>'c','2'=>'y','3'=>'m','4'=>'z','5'=>'a'); 
print_r($arr1+$arr2,1);
print_r($arr1+$arr3,1);
Array (
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)  Array(    [0] => 1    [1] => 2    [2] => 3    [3] => 4    [4] => 5
[5] => a
)

The second example tells you that, by default, php keys are 0, 1, 2, and 3.

When arrays are merged, the appended values of existing keys do not overwrite the original values.

It seems that the value of the last sentence is relatively large. I write a lot of blog nonsense.

The above introduces php array merging, including some content, and 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.