Home Web Front-end JS Tutorial PHP quickly implements array deduplication method

PHP quickly implements array deduplication method

May 11, 2018 pm 03:35 PM
php array method

This time I will bring you a method to quickly implement array deduplication in PHP. What are the precautions to quickly implement array deduplication in PHP? The following is a practical case, let's take a look.

Overview

Using PHP’s

array_unique<a href="http://www.php.cn/wiki/1027.html" target="_blank">()</a> function allows you to pass an array, Then remove duplicate values ​​and return an array with unique values. This function works well most of the time. However, if you try to use the array_unique() function on a large array, it will run slower.

There is a better and faster function

array_flip() instead of using the array_unique() function to create a unique array. This magical function will swap the key and value of each element in the array, because the key value must be unique, therefore, you will get the same result as the array_unique() function.

PHP code:

/* 创建一个包含重复值的,一共四个元素的数组 */
$array = array('green','blue','orange','blue');
/* 翻转数组,你将会得到唯一键值的数组
    array('green'=&gt;0,'blue'=&gt;1,'orange'=&gt;2); */
$array = array_flip($array);
/* 然后再翻转一次,将键和值重新放置,然后得到数组:array(0=&gt;'green',1=&gt;'blue',2=&gt;'orange'); */
$array = array_flip($array);
print_r($array)
Copy after login
Running result:

Array
(
  [0] =&gt; green
  [3] =&gt; blue
  [2] =&gt; orange
)
Copy after login
Because we have removed some elements, the array does not look like 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 keys to remain in numerical order, you can use one or two methods to solve the problem of key values ​​being out of order.

Use array_merge to repair the keys of the array

The function after adding

array_flip will sort the keys of the array and restore them to normal sequence, such as: 0,1,2,3...

PHP code:

$array = array('green','blue','orange','blue');
$array = array_flip($array);
$array = array_flip($array);
/* 使用array_merge()函数修复键值*/
$array = array_merge($array);
print_r($array)
Copy after login
The running result is the same as above

The second way is to use array_keys

Note that this method of repairing array key values ​​is slightly 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 flipped value). Then when you flip the values ​​in the array, the keys are created in order.

PHP code:

$array = array('green','blue','orange','blue');
$array = array_flip($array);
/* 跟第一个例子一样,但是现在我们先提取数组的键值 */
$array = array_keys($array);
print_r($array)
Copy after login
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

JS retains one digit and removes non-digits

JS retains two decimal places for input numbers Verification code

The above is the detailed content of PHP quickly implements array deduplication method. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles