How to remove duplicate values in PHP arrays

Source: Internet
Author: User
This article mainly introduces the PHP array to remove the duplicate values of the method, interested in the friend's reference, I hope to be helpful to everyone.

Overview

Using PHP's Array_unique () function allows you to pass an array, and then remove the duplicate values, returning an array with unique values. This function works very well in most cases. However, if you try to use the Array_unique () function in a large array, it will run slowly.

There is a better and faster function array_flip () instead of using the Array_unique () function to create a unique array. This magical function swaps the keys and values of each element in the array, because the key values must be unique, so you get the same result as the Array_unique () function.

Faster way to implement PHP array de-weight

/* Create an array of four elements with duplicate values */$array = array (' green ', ' blue ', ' orange ', ' blue ');/* Flip the array and you will get an array of unique key values (' green ' =>0, ' Blue ' =>1, ' orange ' =>2); */$array = Array_flip ($array);/* Then flip it again, reposition the keys and values, and get the array: Array (0=> ' green ',1=> ' blue ',2=> ' orange '); */$array = Array_flip ($array);

Because we have removed some elements, the array does not appear to be a normal sequence. For example we may get: Array (0=> ' A ',2=> ' B ',5=> ' C ');. In some cases, this is not a problem, but if you need the array's key values to keep the sequence of numbers, you can use one or two methods to solve the problem of key-value disorder.

Keys for repairing arrays using Array_merge

After adding Array_flip functions, the key values of the array will be sorted and restored to the normal sequence, such as: 0,1,2,3 ...

$array = Array (' green ', ' blue ', ' orange ', ' Blue '), $array = Array_flip ($array); $array = Array_flip ($array);/* Use Array_ The merge () function fixes the key value */$array = Array_merge ($array);

The second way, use Array_keys

Note that this method of repairing array key values is a little faster than using the Array_merge () function. You can also use the Array_keys () function in conjunction with the last step (this function returns the value after the rollover). Then when you flip the values of the array, the key values are created in order.

$array = Array (' green ', ' blue ', ' orange ', ' Blue '), $array = Array_flip ($array);/* As in the first example, but now let's extract the array's key value */$array = array _keys ($array);

Summary : The above is the entire content of this article, I hope to be able to help you learn.

Related recommendations:

PHP implementation of the public number automatically send red envelopes API

PHP e-Mail Verification instance detailed

PHP for loop using methods and examples of detailed

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.