Php-array_merge and + Difference

Source: Internet
Author: User

for +, when the key is at the same time, regardless of whether the key is a character or an array, it discards the subsequent result ;
Array_merge, when key is at the same time, key is a character, then the previous one is overwritten, the key is a number, and no overwrite occurs, and the array index is re-established.

<?php $arr 1 = array ( ' a ' => ' PHP ');  $arr 2 = array ( ' a ' => ' JAVA '); //if the key name is a character and the key name is the same, the value of the array element after Array_merge () overrides the previous array element value Print_r (Array_merge (  $arr 1, $arr 2)); //array ([a] = JAVA) //if the key name is a character and the key name is the same, The addition of the array will result in the first occurrence of the value Print_r ( $arr 1+ $arr 2); //array ([a] = PHP) ?>      

If the key name is a number, the difference:  

<?php $arr 1 = array (" C ",  "PHP");  $arr 2 = array ( "JAVA",  "PHP"); //if the key name is a number, array_merge () will not overwrite Print_r (Array_merge ( $arr 1,  $arr 2)); //array ([0] = C [1] + php [2] = + JAVA [3] = PHP) // If the key name is an array, the sum of the arrays will be the first occurrence of the value as a result, the following key name will be discarded Print_r ( $arr 1+$ ARR2); //array ([0] = C [1] = PHP) ?> 

" how to get the most efficient set of two array values with the same character key name and different values", array_merge_recursive-recursively merges one or more arrays, if the input array has the same string key name, The values are merged into an array .

<?php

 $arr 1 = array ( "a" => "php",  "C");  $arr 2 = array ( "a" => "Java", Span class= "hljs-string" > "C",  "Ruby");p Rint_r (Array_merge_recursive (  $arr 1,  $arr 2)); ?> results are as follows: array ([a] =  Array ([0] + php [1] + java) [0 ] = c [1] + c [2] + Ruby)     /span>                
 

Php-array_merge and + Difference

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.