Comparison of php one-dimensional array merging Methods

Source: Internet
Author: User
There are three ways to merge php Arrays: array_merge, array_merge_recursive, + (the plus sign of the mathematical operator). The following is a comparison code $ array1array (2, 4, colorred); $ array2array (a, B, colorgreen, shapetrapezoid, 4); $ resultarray_merge ($ array

There are three ways to merge php Arrays: array_merge, array_merge_recursive, + (the plus sign of the mathematical operator). The following is a comparison code $ array1 = array (2, 4, "color" = "red"); $ array2 = array ("a", "B", "color" = "green", "shape" = "trapezoid ", 4); $ result = array_merge ($ array

There are three ways to merge php Arrays: array_merge, array_merge_recursive, + (that is, the plus sign of the mathematical operator );
The following is a comparison code.

    $array1 = array(2,4,"color" => "red");    $array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);    $result = array_merge($array1, $array2);    echo "----------------array_merge---------------".PHP_EOL;    print_r($result);    echo "----------------+++++++++++---------------".PHP_EOL;    print_r($array1+$array2);    echo "----------------array_merge_recursive---------------".PHP_EOL;    print_r(array_merge_recursive($array1,$array2));

The result is as follows:

----------------array_merge---------------Array(    [0] => 2    [1] => 4    [color] => green    [2] => a    [3] => b    [shape] => trapezoid    [4] => 4)----------------+++++++++++---------------Array(    [0] => 2    [1] => 4    [color] => red    [shape] => trapezoid    [2] => 4)----------------array_merge_recursive---------------Array(    [0] => 2    [1] => 4    [color] => Array        (            [0] => red            [1] => green        )    [2] => a    [3] => b    [shape] => trapezoid    [4] => 4)

Compare the "color" values of array_merge and + and the results of array_merge_recursive, we can see that:
1. For the same string index,
Array_merge uses the following value to overwrite the preceding value;
+ The same key is overwritten with the previous value;
Array_merge_recursive places the same index in an array to add the array dimension;
2. For the same numeric index,
Array_merge will re-index the repeated values (Index value starts from 0);
+ The subsequent values are still overwritten with the previous values;
Array_merge_recursive is the same as array_merge.

Original article address: Comparison of php one-dimensional array merging methods, thanks to the original author for sharing.

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.