Parsing PHP function Array_flip () in repeating array element deletion _php tutorial

Source: Internet
Author: User
As we all know, there are many ways to delete array elements in PHP, and the functions used frequently are PHP array_unique (). So the PHP function Array_flip () We're going to introduce to you today is about 5 times times more efficient than the array_unique () function when you delete an array of repeating elements.
php function Array_flip () format:
Copy CodeThe code is as follows:
Array Array_flip (array trans)
Array_flip--Exchanging keys and values in an array

Array Array_flip (array trans)//array_flip--swap keys and values in an array
Here's how:
Copy CodeThe code is as follows:
$arr = Array (...);//Suppose there are arrays of 10,000 elements with duplicate elements in them.
$arr = Array_flip (Array_flip ($arr)); This allows you to remove duplicate elements.

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:
Copy CodeThe code is as follows:
$arr 1 = Array ("Age" = +, "name" = "Home of the Script");
$arr 2 = array_flip ($arr 1); $arr 2 is the array ("Age", "script House" = "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:
Copy CodeThe code is as follows:
$arr 1 = Array ("Age" = +, "name" = "Home of the Script", "age" = 20); "Age" = 20 will replace "age" + 30
$arr 1 = Array ("name" = "Home of the Script", "age" = 20);

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:
Copy CodeThe code is as follows:
$arr 1 = Array ("Age" = +, "name" = "Home of the Script", "age" = 20);
$arr 1 = array_flip ($arr 1); $arr 1 became an array ("Home of the Script" = "name", "Age");
Then the key name and the value of the $arr 1 are also complex:
$arr 1 = array_flip ($arr 1);

The code for PHP function Array_flip () above is written in a concise way:
Copy CodeThe code is as follows:
$arr 1 = array_flip (Array_flip ($arr 1));

http://www.bkjia.com/PHPjc/327873.html www.bkjia.com true http://www.bkjia.com/PHPjc/327873.html techarticle As we all know, there are many ways to delete array elements in PHP, and the functions used frequently are PHP array_unique (). So today we introduce the PHP function Array_flip () in ...

  • 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.