Array_flip (array);
Passes an array parameter, flipping the key and value of the array
For example:
$a Array ( ' a ', ' B ', ' C '); Print_r (array_flip($a)); // the output is: Array ( = 0 = 1 = 2)// Note: Array_flip (): Can only flip STRING and INTEGER values
Array_merge (Array1,array2[,aray3 ...])
Merging two or more arrays (it is also possible to pass an array parameter)
It is important to note that:
- If there are no key names in the array, then the final result will be returned with an array of 0 starting with the order of the values Array1, array2, regardless of whether there is a duplicate of their value.
- If a key name is set in the array and the key name is duplicated, the key value is the value corresponding to the last key name (overwrite front)
- If the array is a specified numeric index, the key name of the result is re-indexed in an array starting from 0
- If the key name in the array has more than one combination of the above, the key name is explicitly specified as the first
- To summarize, he would like to ensure that the merged array index cannot be duplicated, if the key name is not specified or the specified key name is a number, the result is a number starting from 0, if a non-numeric index is specified, he retains the previous index, but if there are more than one, only the last
A few examples:
//No Index specified$a=Array( ' A ', ' B ',);$b=Array( ' A ', ' B ',);$arr=Array_merge($a,$b);//Output:Array( [0] = =A [1] = =b [2] = =A [3] = =b)//=======================//specifying numeric key names explicitly$a=Array( 3=> ' A ', 4=> ' B ',);$b=Array( 1=> ' A ', 2=> ' B ',);$arr=Array_merge($a,$b);//Output:Array( [0] = =A [1] = =b [2] = =A [3] = =b)//=======================//explicitly specifying numeric key names and non-indexed blends$a=Array( 3=> ' A ', ' B ',);$b=Array( ' A ', 2=> ' B ',);$arr=Array_merge($a,$b);//Output:Array( [0] = =A [1] = =b [2] = =A [3] = =b)//=======================//explicitly specifying a non-numeric index 1$a=Array( ' A ' =>1, ' B ',);$b=Array( ' A ', ' B ',);$arr=Array_merge($a,$b);//Output:Array([a]= 1 [0] = =b [1] = =A [2] = =b)//=======================//explicitly specifying a non-numeric index 2$a=Array( ' A ' =>1, ' B ' =>2,);$b=Array( ' A ', ' B ' =>22,);$arr=Array_merge($a,$b);//Output:Array([a]= 1[b]= 22 [0] = =a)
Several other combinations of examples are not posted up ...
Array1+array2
This kind of writing is actually useless, studied a, he is actually used to merge array operations. There is a certain difference between usage and array_merge.
- If the array does not specify an index, then he will first assign the array1 to the result, assuming that the array1 length is 3,array2 length of 5, then he will array2[3], array2[4] append to the result, array2[0-2] directly ignore
- If an array is specified for an index, whether numeric or non-numeric, the final result retains the specified index, and if there are duplicate indexes on the two arrays, the first is the final result (overwriting later)
- In the case of a specified index and no index mix, first look at Array1, if the first few are non-indexed, such as the first 3 are not specified, then also according to Rule 1, ignore the array2 in the first 3. If ARRAY1[0] is explicitly specified, the array elements in Array2 are not ignored, unless the same index is specified, followed by rule 2
A few examples:
//No Index specified$a=Array( ' A ', ' B ',);$b=Array( ' C ', ' d ', ' e ');$arr=$a+$b;//Output:Array( [0] = =A [1] = =b [2] = =e)//=======================//Specifying a numeric index$a=Array( 1=> ' A ', 2=> ' B ',);$b=Array( 4=> ' C ', 7=> ' d ');$arr=$a+$b;//Output:Array( [1] = =A [2] = =b [4] = =C [7] = =d)//=======================//The specified index, there is a duplicate$a=Array( ' A ' =>11, ' B ' =>22,);$b=Array( ' C ' =>33, ' d ' =>44, ' a ' =>55);$arr=$a+$b;//Output:Array([a]= 11[b]= 22[C]= 33[d]= 44)//=======================//mixed specified index and no index 1$a=Array( ' A ', 2=> ' B ',);$b=Array( ' C ', ' d ', 10=> ' a ', 4=>22);$arr=$a+$b;//Output:Array( [0] = =A [2] = =b [1] = =d [+] =A [4] = 22)//=======================//mixed specified index and no index 2$a=Array( 2=> ' B ', ' a ',);$b=Array( ' C ', ' d ', 10=> ' a ', 4=>22);$arr=$a+$b;//Output:Array( [2] = =b [3] = =A [0] = =C [1] = =d [+] =A [4] = 22)
Use Array_flip with Array+array (remove duplicate values from two arrays):
$a=Array( ' A ', ' B ', ' C ');$b=Array( ' A1 ', ' B1 ', ' C ');$arr=Array_flip($a) +Array_flip($b);Print_r(Array_keys($arr));//Output:Array( [0] = =A [1] = =b [2] = =C [3] = =A1 [4] = =B1)
Summary of usage of PHP array_flip () array_merge () Array+array