Php merge array key value and retain key value implementation method, php merge array key value

Source: Internet
Author: User

Php merge array key value and retain key value implementation method, php merge array key value

Php merges arrays.Array_mergeMethod.

Array_merge-merge one or more Arrays

array array_merge ( array $array1 [, array $... ] )

Array_merge combines the units of one or more arrays. The values in an array are appended to the previous array and returned as the result array.

If the input array contains the same string key name, the value after the key name overwrites the previous value. However, if the array contains a number key name, the subsequent values will not overwrite the original values, but will be appended to the back.

If only one array is assigned and the array is indexed by number, the key name is re-indexed continuously.

Example 1: The array uses the string key name. The same key name will be overwritten later.

<?php$arr1 = array('name'=>'fdipzone');$arr2 = array('name'=>'terry');$result = array_merge($arr1, $arr2);print_r($result);?>

Output:

Array( [name] => terry)

Example 2: The array uses the number key name. If the key name is the same, it will not be overwritten and the key name will be re-indexed.

<?php$arr1 = array(0=>'fdipzone',1=>'terry');$arr2 = array(0=>'php',1=>'python');$result = array_merge($arr1, $arr2);print_r($result);?>

Output:

Array( [0] => fdipzone [1] => terry [2] => php [3] => python)

Because of work requirements, you need to combine the answers to the two multiple-choice questions of the questionnaire. Each part of the answers is an array (key => value), key is the question number, and value is the answer, the question numbers of the two parts do not exist.

Merge two answers using 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);print_r($result);?>

Output:

Array( [0] => A [1] => B [2] => C [3] => D [4] => B [5] => A [6] => D [7] => C)

Use array_merge to merge. Because the question number (key) is a number, the key name is re-indexed, and the question number cannot be retained.

Merge the array and retain the key value:

<?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( [11] => A [12] => B [13] => C [14] => D [25] => B [26] => A [27] => D [28] => C)

Use"+"Operators merge arrays to retain the key values of the array. If the merged array contains the same key value, the subsequent values do not overwrite the previous key value (the previous one takes precedence ).

The above php merge array and retain the key value implementation method is the small series to share with you all the content, hope to give you a reference, but also hope you can support a lot of help home.

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.