Parsing the function of PHP function Array_flip () in repeating array element deletion _php tips

Source: Internet
Author: User

We all know that there are many kinds of ways to delete array elements in PHP, and the functions that are often used are PHP array_unique (). So the PHP function we introduced today Array_flip () is about 5 times times more efficient than the array_unique () function when deleting an array of duplicate elements.
php function Array_flip () format:

Copy Code code as follows:

Array Array_flip (array trans)
Array_flip--Exchanging keys and values in an array

Array Array_flip (array trans)//array_flip--exchanging keys and values in an array
The method is as follows:
Copy Code code as follows:

$arr = Array (...);//Suppose there are 10,000 elements of the arrays, which have duplicate elements.
$arr = Array_flip (Array_flip ($arr)); This allows you to delete duplicate elements.

What the hell is going on here? Look at the role of Array_flip (): Php function Array_flip () is used to exchange the keys and values of each element of an array, such as:
Copy Code code as follows:

$arr 1 = Array ("Age" =>, "name" => "cloud-dwelling community");
$arr 2 = array_flip ($arr 1); $arr 2 is the array (=> "age", "cloud-dwelling community" => "name");

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

$arr 1 = Array ("Age" =>, "name" => "cloud-dwelling community", "age" => 20); "Age" => 20 will replace "age" => 30
$arr 1 = Array ("name" => "cloud-dwelling community", "age" => 20);

Here $arr 1 is equal to the $ARR2.
Then we can see why Array_flip (Array_flip ($arr) can delete the duplicated 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:
Copy Code code as follows:

$arr 1 = Array ("Age" =>, "name" => "cloud-dwelling community", "age" => 20);
$arr 1 = array_flip ($arr 1); $arr 1 became array ("cloud-dwelling community" => "name", => "age");
Then the key name of the $arr 1 and the value are also complex:
$arr 1 = array_flip ($arr 1);

The code for the PHP function Array_flip () above is written in a concise way:
Copy Code code as follows:

$arr 1 = array_flip (Array_flip ($arr 1));

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.