As we all know,php function Array_flip () format:
Array Array_flip (array trans)//array_flip--swap keys and values in an array
Here's how:
- $ arr array(...);//Suppose there are arrays of 10,000 elements with duplicate elements in them.
- $ arr Array_flip
What the hell is going on here? Take a look at the role of Array_flip (): The PHP function Array_flip () is used to exchange keys and values for each element of an array, such as:
- $ arr1 Array ("Age" => , "name" => "PHP self-Study net") ;
- $ arr2 array_flip($arr 1);//$arr 2 is Array ( > "Age ", "PHP self-study Net" = > "name");
In an array of PHP, allow different elements to take the same value, but not allow the same key name to be used by different elements, such as:
- $ arr1 Array ("Age" => , "name" => "PHP self-study net", "age" = > 20); "Age" => 20 will replace "age" = >
- $ arr1 Array ("name" => "PHP self-study net", "age" => 45) ;
Here $arr 1 are equal to the $ARR2.
As a result, we can see why Array_flip (Array_flip ($arr)) can delete duplicate elements in the array. First, the value in the $arr becomes the key name, because the value is duplicated, the duplicate value becomes the duplicate key name after the key name, and the PHP engine deletes the duplicate key name, leaving only the last one. Such as:
- $ arr1 = array ("Age" = ; 30 , "name" = "PHP self-study net", "age" = ; 20);
- $ arr1 = array_flip ($arr 1);//$arr 1 becomes an array (" PHP Self-study Net "= > "name", 20 = "age");
- //re-$arr 1 Key name and value:
- $ arr1 = array_flip ($arr 1);
The code for PHP function Array_flip () above is written in a concise way:
- $ arr1 array_flip(Array_flip ($arr 1));
http://www.bkjia.com/PHPjc/446336.html www.bkjia.com true http://www.bkjia.com/PHPjc/446336.html techarticle as we all know, PHP function array_flip () format: Arrayarray_flip (Arraytrans)//array_flip--swap keys and values in array array_flip (array trans)// Array_flip--swap array ...