PHP Merge Arrays

Source: Internet
Author: User
This article mainly introduces the PHP merged array, has a certain reference value, now share to everyone, the need for friends can refer to

Methods for merging arrays

    1. Array_merge: Number keys, add directly back, key reset string key, the value of the following array will replace the previous value +: Number key, the value of the following array will not replace the previous value string key, the value of the subsequent array will override the previous value

Merge arrays and go back to

1. The singular group to repeat    Array_unique ($arrTest)//2. Most groups go to repeat    Array_keys (array_flip ($arr 1) +array_flip ($arr 2))
PHP Array_merge Merge Method Example 1, the array uses the string key name, the same key name will be overwritten by the following
<?PHP$ARR1 = Array (' name ' = ' Fdipzone '); $arr 2 = Array (' name ' = ' Terry '); $result = Array_merge ($arr 1, $arr 2); Print_r ($result);? >

Output:

Array (    [name] = Terry)

Example 2, array using Numeric key name, the same key name is not overwritten, and the key name is re-indexed

<?PHP$ARR1 = Array (0=> ' Fdipzone ',1=> ' Terry '); $arr 2 = Array (0=> ' php ',1=> ' python '); $result = Array_ Merge ($arr 1, $arr 2);p Rint_r ($result);? >

Output:

Array (    [0] = Fdipzone    [1] = Terry    [2] = + PHP    [3] = = Python)

Combine two-part answers with Array_merge

<?php$form_data1 = Array (11=> ' A ',12=> ' B ',13=> ' C ',14=> ' D '); $form _data2 = Array (25=> ' B ',26=> ' A ' ,27=> ' D ',28=> ' C '); $result = Array_merge ($form _data1, $form _data2);p rint_r ($result);? >

Output

Array (    [0] = a    [1] = b    [2] = = C    [3] = + D    [4] = B    [5] = = a    [6] = d
  
   [7] = C)
  

Methods for merging arrays and preserving key values:

<?php$form_data1 = Array (11=> ' A ',12=> ' B ',13=> ' C ',14=> ' D '); $form _data2 = Array (25=> ' B ',26=> ' A ' ,27=> ' D ',28=> ' C '); $result = $form _data1 + $form _data2;print_r ($result);? >

Output:

Array (    [one] = a    [] = b    [all] = C    [+] = D    [+] = b    [+] + a    [] => ; D    [+] = C)
$arr = [' A ' =>12, ' B ' =>13]; $arr 1 = [' A ' =>14, ' b ' =>15,0=>1,1=>2]; $fild = $arr + $arr 1;
Print_r ($fild); Array ([A] = [b] = [0] = 1 [1] = 2)

Using the "+" operation conforms to the array, you can preserve the key values of the array, if the merged array contains the same key value, the following will not overwrite the previous key value (keep the previous value, discard the following).
Related recommendations:

2 ways to merge arrays in PHP

PHP merges two one-dimensional arrays

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.