PHP combined with 2 numeric key array values Program _php tutorial

Source: Internet
Author: User

PHP Merge 2 numeric key array values of the program


In PHP, the array_merge is the most reliable function, the following we use the Array_merge function when merging is mainly for the two array of key values for the processing of numbers.

To understand a basic point of knowledge first

The combination of two arrays in PHP can use + or array_merge, but there is still a difference between them, and these differences if you do not know clearly the project will be fatal!

The main difference is that if the same key name appears in two or more arrays, the key name is a string or a number, you need to be aware

1) When the key name is a number, the value after Array_merge () will not overwrite the original value, but append it to the back, but the + merge array will return the first occurrence as the final result, and the values of the following array with the same key name "Discard" (not overwrite)

2) When the key name is a string, Array_merge () overwrites the previous value of the same key name, but + still returns the first occurrence as the final result, and "discards" the values of the following array with the same key name (not overwrite).

The code is as follows

$a = Array (' A ', ' B ');
$b = Array (' C ', ' d ');
$c = $a + $b;
Var_dump ($a);
Var_dump (Array_merge ($a, $b));

Output:

array
0 = String ' a ' (length=1)
1 = String ' B ' (length=1)

Array
0 = String ' a ' (length=1)
1 = string ' B ' (length=1)
2 = String ' C ' (length=1)
3 = String ' d ' (length=1)

++++++++++++++++++++++++++++++++++++++++++

$a = Array (
0 = ' a ',
1 = ' B '
);
$b = Array (
0 = ' C ',
1 = ' B '
);
$c = $a + $b;
Var_dump ($c);
Var_dump (Array_merge ($a, $b));

Output:

array
0 = String ' a ' (length=1)
1 = String ' B ' (length=1)

Array
0 = String ' a ' (length=1)
1 = string ' B ' (length=1)
2 = String ' C ' (length=1)
3 = String ' B ' (length=1)

++++++++++++++++++++++++++++++++++++++++++

$a = Array (' A ', ' B ');
$b = Array (
' 0 ' = ' C ',
1 = ' B '
);
$c = $a + $b;
Var_dump ($c);
Var_dump (Array_merge ($a, $b));

//output:
array
0 = String ' a ' (length=1)
1 = String ' B ' (length=1)

Array
0 = String ' a ' (length=1)
1 = string ' B ' (length=1)
2 = String ' C ' (length=1)
3 = String ' B ' (length=1)

++++++++++++++++++++++++++++++++++++++++++

$a = Array (
0 = ' a ',
1 = ' B '
);
$b = Array (
' 0 ' = ' C ',
' 1 ' = ' B '
);
$c = $a + $b;
Var_dump ($c);
Var_dump (Array_merge ($a, $b));

Output:

array
0 = String ' a ' (length=1)
1 = String ' B ' (length=1)

Array
0 = String ' a ' (length=1)
1 = string ' B ' (length=1)
2 = String ' C ' (length=1)
3 = String ' B ' (length=1)

All right, so much for this, let's take a look at the value of merging 2 numeric key arrays

The code is as follows

/**
* PHP combined 2 numeric key array values
*
* @param array $arr 1
* @param array $arr 2
* @return Array
* @author www.111cn.net
*/
function New_array_merge ($arr 1, $arr 2)
{
$arr = Array_flip ($arr 1) + Array_flip ($arr 2);
Return Array_keys ($arr);
}

$arr 1 = array (' AA ', ' BB ', ' cc ');
$arr 2 = Array (' Aa2 ', ' BB ', ' cc2 ');

$arr = New_array_merge ($arr 1, $arr 2);
Print_r ($arr);

Output:

Array
(
[0] = = AA
[1] = BB
[2] + = CC
[3] = Aa2
[4] = CC2
)

http://www.bkjia.com/PHPjc/917546.html www.bkjia.com true http://www.bkjia.com/PHPjc/917546.html techarticle PHP Merge 2 numeric key array of values of the program PHP array merge Array_merge is the most reliable function, the following we in the use of Array_merge function merging is mainly for two arrays of keys ...

  • 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.