How to make multiple arrays of PHP Merge "code sample"

Source: Internet
Author: User
This article will give you a detailed introduction to the specific method of merging PHP arrays. First of all, for PHP scholars, we all know that arrays are very important in PHP, and arrays are not a small part of a complete PHP project. For PHP beginners, it is not difficult to create arrays, but it may be difficult to perform complex operations on a specific array.





For example, we encountered in the project need to merge two arrays or multiple arrays into an array of cases, in fact, as long as the knowledge of the relevant functions, the future of such problems can be handy. Here are some examples of how PHP arrays are merged with specific code samples.



Note: There are three types of arrays, indexed arrays; associative arrays; multidimensional arrays.



An example of an array merge code is as follows:



First, PHP merge two ordinary arrays



<? php
header ("content-type: text / html; charset = utf-8"); // Set the encoding
?>
<? php
$ ar1 = array ('iphone', 'Samsung');
$ ar2 = array ('Glory', 'Nokia');
print_r (array_merge ($ ar1, $ ar2));
?>


There are two ordinary arrays in the code above, and we combine the two numbers with the Array_merge function and print out the results such as:






It is clear that 2 arrays have been successfully merged into an array. I believe we all notice the array_merge function , yes, thearray_merge function is the ability to merge multiple arrays.



Two, PHP merge two associative arrays



<? php
$ ar1 = array ('a' => 'iphone', 'b' => 'Samsung');
$ ar2 = array ('d' => 'Glory', 'e' => 'Nokia');
print_r (array_merge ($ ar1, $ ar2));
?> 


Merges two associative arrays and accesses the results through a browser such as:






At this time, do not know whether people find a problem? The two associative arrays have an identical key name, and after merging, the next key name value overrides the additional value. That is, if you have the same key name in multiple arrays, then multiple array merges with the Array_merge function will have one element covering the other.



At the end of the note, we also show you another use of the array_merge merge function, that is, if we put an array with a key name as an integer and enter it into this function, we will get a new array that is re-indexed!



So the above is about PHP merging 2 arrays and multiple array of data specific methods introduced, which mainly require you to understand the Array_merge function. This article has certain reference value, hoped to have the need friend to have the help!


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.