PHP Array Merging

Source: Internet
Author: User
PHP's array, and Java is a big gap, although the weak type of PHP design may be a lot of Java, C + + programmers are criticized, but I think in the array container design, PHP go far than Java and C + +.

In addition to the incomplete problem, Java's container design allows for the existence of an array of C + +, which is the only place in the Java language itself that does not satisfy all objects.

In fact, the existence of the container itself is to reduce certain efficiency, but if the excessive emphasis on efficiency, why not simply use machine code it? So I don't really like the idea of getting too far away from the PHP container design.

As we all know, the array of PHP itself is a map.

For a normal array, its internal structure is actually:

$arr 1=array (1,2,3,4,5);p rint_r ($arr); Array (    [0] = 1    [1] = 2    [2] = 3    [3] = 4    [4] = 5)

For the map we usually understand, PHP is implemented internally:

$arr 2=array(' C ' =>1, ' y ' =>2, ' m ' =>3); Print_r ($arr 2, 1Array(    = 1    = 2    = > 3)

For array merging, you can assume that PHP uses an overloaded feature like C + + that overloads the plus sign:

Print_r ($arr 1+$arr 2, 1); Array (    [0] = 1    [1] = 2    [2] = 3    [3] = 4    [ /C13>4] = 5    = 1    = 2    = 3)

This array merge is convenient for you to traverse this array from the beginning, because in many cases you will not use the array in random access, but rather walk from the beginning to the end.

However, one of the things you can easily overlook here is that the array is essentially a map. If two arrays have the same key, it is easy for you to ignore them:

$arr 1=array(1,2,3,4,5); $arr 2=array(7,8,9); $arr 3=array(' 1 ' = ' C ', ' 2 ' = ' y ', ' 3 ' + ' m ', ' 4 ' = ' = ' z ', ' 5 ' = ' a ')
Print_r ($arr 1+$arr 2, 1);
Print_r ($arr 1+$arr 3, 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 I mean is to tell you that, by default, the PHP key is 0,1,2,3.

For an array merge, the appended value does not overwrite the previous value for the existing key.

It seems that only the last sentence is of greater value. I write a blog is more nonsense.

The above describes the PHP array merge, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • 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.