A few days ago, I wrote a function analysis about Array_merge.
Today, let's see a new function Array_combine ()
There are two parameters in this function, one is the key name of the merged array, and the other is the key value.
Note: After merging, the key name of the array is placed on the first parameter bit, and the key value is placed on the second parameter bit.
Two parameters are not allowed to be defaulted.
The number of elements in the two parameter array must be the same otherwise an error will be given.
If either parameter array is empty, the same error will occur.
Practice the truth, let the code run up ~
The correct way to open 1.array_combine ():
1 <?php 2 $key = array (' Boom ', ' Tom ', ' Jack '); 3 $values = Array (' n ', ' OK ', ' 1017 '); 4 $ret = Array_combine ($key, $values); 5 Print_r ($ret); 6 7 8//run result 9 Array11 ( [boom] = 1213 [Tom] + ok14 [jack] = 101715)
2. What happens if the elements of the parameter array of the key name are the same?
According to the following example, we will find that if the same element is present, the latter one will overwrite the previous value.
This is especially important in the development process, as it can result in incomplete data results.
1<?PHP2 $key=Array(' Boom ', ' Tom ', ' Tom ');3 $values=Array(' K ', ' OK ', ' 1017 ');4 $ret=Array_combine($key,$values);5 Print_r($ret);6 7 8 //Run result9 ArrayTen ( One[Boom] = 12 A[Tom] = 1017 -)
According to the above example, I do not think if the elements of the two parameter array have their own key name will be what kind of situation?
3. When the first parameter array (the key name of the merged array) has a custom key name
According to the following demerit we will find the same results as in Example 1.
1 <?php 2 $key = Array (' a ' = = ' boom ', ' b ' = ' tom ', ' C ' + = ' jack '); 3 $values = Array (' n ', ' OK ', ' 1017 '); 4 $ret = Array_combine ($key, $values); 5 Print_r ($ret); 6 7//run result 8 9 Array10 (All [boom] = 1212 [Tom] + ok13 [jack] + 101714)
4. When a second parameter array (the key value of the merged array) has a custom key name
We found the same as before.
<?php$key = Array (' Boom ', ' Tom ', ' Jack '); $values = Array (' a ' = ' + ', ' b ' = ' OK ', ' c ' = ' 1017 '); $ret = a Rray_combine ($key, $values);p Rint_r ($ret);//run resultarray ( [boom] [ tom] = OK [Jack] = 1017)
5. When two parameter arrays have a custom key name
<?php$key = Array (' 1 ' = ' boom ', ' 2 ' = ' Tom ', ' 3 ' = ' Jack '); $values = Array (' a ' = ' + ', ' B ' + ' = ' OK ' C ' = ' 1017 '); $ret = Array_combine ($key, $values);p Rint_r ($ret);//run resultarray ( [boom] [ Tom] = = OK [jack] = 1017)
In summary: Array_combine () The result of this function is only affected by the parameter array key value, regardless of the key name.
Furthermore, overrides occur if the parameter array element is the same.
This article for fennel dumplings Blog original, welcome reprint, reproduced please indicate the source
Fennel Dumplings Blog http://www.cnblogs.com/hxjz/
Deep parsing of PHP array functions array_combine