Home >Backend Development >PHP Tutorial >Analyze the role of PHP function array_flip() in deleting duplicate array elements_PHP tutorial

Analyze the role of PHP function array_flip() in deleting duplicate array elements_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:03:181367browse

We all know that there are many ways to delete array elements in PHP. The commonly used function is array_unique() in PHP. Then the PHP function array_flip() we introduce to you today is about 5 times more efficient than the array_unique() function in deleting duplicate elements from an array.
PHP function array_flip() format:

Copy code The code is as follows:

array array_flip ( array trans )
//array_flip -- Swap the keys and values ​​in the array

array array_flip ( array trans ) //array_flip -- Swap the keys and values ​​in the array
method As follows:
Copy code The code is as follows:

$arr = array(…………);//Suppose there is a An array of 10,000 elements contains duplicate elements.
$arr = array_flip(array_flip($arr)); //This way duplicate elements can be deleted.

What on earth is going on? Let’s take a look at the function of array_flip(): The PHP function array_flip() is used to exchange the key and value of each element of an array, such as:
Copy code The code is as follows:

$arr1 = array ("age" => 30, "name" => "Script Home");
$arr2 = array_flip($arr1) ; //$arr2 is array(30 => "age", "Script Home" => "name");

In the PHP array, different elements are allowed to be taken The same value, but the same key name is not allowed to be used by different elements, such as:
Copy code The code is as follows:

$arr1 = array ("age" => 30, "name" => "Script Home", "age" => 20); "age" => 20 will replace "age" => ; 30 
$arr1 = array ("name" => "Script Home", "age" => 20);

Here $arr1 and $arr2 are equal.
So, we can know why array_flip(array_flip($arr)) can delete duplicate elements in the array. First, the value in $arr will become a key name, because the value is repeated. After becoming a key name, these repeated values ​​will become duplicate key names. The PHP engine will delete the duplicate key names and only keep the last one. . For example:
Copy code The code is as follows:

$arr1 = array ("age" => 30, "name" => "Script Home", "age" => 20);
$arr1 = array_flip($arr1); //$arr1 becomes array("Script Home" => "name" , 20 => "age");
//Restore the key name and value of $arr1:
$arr1 = array_flip($arr1);

The above PHP The code of the function array_flip() is written more concisely:
Copy the code The code is as follows:

$arr1 = array_flip(array_flip ($arr1));

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327873.htmlTechArticleWe all know that there are many ways to delete array elements in PHP, and the commonly used functions are php in array_unique(). So the PHP function array_flip() we introduce to you today is...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn