How to use PHP to combine arrays and preserve key values

Source: Internet
Author: User

PHP merges arrays, typically using the array_merge method.

array_merge -merge one or more arrays

Array Array_merge (array $array 1 [, Array $ ...])

Array_merge merges the cells of one or more arrays, and the values in an array are appended to the previous array, returning the array as the result.

If the input array has the same string key name, the value following the key name overrides the previous value. However, if the array contains numeric key names, the following will not overwrite the original value, but append to the back.

If only one array is given and the array is a numeric index, the key name is re-indexed in a sequential manner.
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, the array uses a 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)


As a result of the work needs, the two parts of the questionnaire will need to answer the answers together, each part of the answer is an array (key=>value), key is the title number, value is the answer, and the two parts of the problem number does not exist duplicate.

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)
  

Use array_merge Merge, because the title number (key) is a number, so the key name is re-indexed, resulting in the problem number cannot be persisted.

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    [+] = C    [+] = D    [+] = b    [+] = a    [2 7] = + D    [+] = C)


Using the "+" operation conforms to the array, you can preserve the key values of the array, and if the merged array contains the same key values, the previous key values will not be overwritten (previous precedence).

This article explains how to use PHP to combine arrays and preserve key values, and more on the PHP Chinese web.

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.