【译】更快的方式实现PHP数组去重
原文: Faster Alternative to PHP’s Array Unique Function
概述
使用PHP的 array_unique()函数允许你传递一个数组,然后移除重复的值,返回一个拥有唯一值的数组。这个函数大多数情况下都能工作得很好。但是,如果你尝试在一个大的数组里使用 array_unique()函数,它会运行地慢一些。
有一个比较好而且更快的函数 array_flip()来替代使用 array_unique()函数来创建唯一的数组。这个魔法般的函数会交换数组里面每一个元素的键和值,因为键值必须唯一,因此,你会得到跟 array_unique()函数一样的结果。
更快的方式实现PHP数组去重
/* 创建一个包含重复值的,一共四个元素的数组 */$array = array('green','blue','orange','blue');/* 翻转数组,你将会得到唯一键值的数组 array('green'=>0,'blue'=>1,'orange'=>2); */$array = array_flip($array);/* 然后再翻转一次,将键和值重新放置,然后得到数组:array(0=>'green',1=>'blue',2=>'orange'); */$array = array_flip($array);
因为我们已经移除了一些元素,因此数组看起来不是正常的序列。比如我们可能会得到: array(0=>’A’,2=>’B’,5=>’C’);。在某些情况下,这不是一个问题,但是如果你需要数组的键值保持数字的序列,你可以使用一到两种方法解决键值乱序的问题。
使用array_merge修复数组的keys
添加array_flip之后的函数,将会对数组的键值排序并且让它们恢复到正常的序列,如:0,1,2,3…
$array = array('green','blue','orange','blue');$array = array_flip($array);$array = array_flip($array);/* 使用array_merge()函数修复键值*/$array = array_merge($array);
第二种方式,使用array_keys
注意,这种修复数组键值的方法比使用 array_merge()函数稍微快了一点。你也可以在最后一步结合使用 array_keys()函数(此函数返回翻转后的值)。然后当你翻转数组的值,键值就会根据顺序创建。
$array = array('green','blue','orange','blue');$array = array_flip($array);/* 跟第一个例子一样,但是现在我们先提取数组的键值 */$array = array_keys($array);
结论
非常简单,比起在大数组使用array_unique函数,有了一个有效的性能提升。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
