php array_flip()与array_uniqure()删除数组重复元素
在php中删除数组重复元素的函数有几个,一个是array_unique()另外就是array_flip()与array_uniqure()函数,但后者比前者性能要高几倍了,所以我只介绍两者来删除重复数组元素了。
方法如下:
代码如下 | 复制代码 |
$arr = array(…………) ;// 假设有数组包含一万个元素,里面有重复的元素。 |
究竟是怎么回事呢?来看下array_flip()的作用:array_flip()用于将一个数组的每个元素的键和值交换,如:
代码如下 | 复制代码 |
|
在PHP数组中,允许不同的元素可以取同一个值,但不允许同一个键名被不同的元素使用,如:
代码如下 | 复制代码 |
|
这里 $arr1与$arr2 是相等的。
于是,我们便可以知道,为什么 array_flip(array_flip($arr)) 可以删除数组中重复的元素了。首先,$arr里的值会变成键名,因为值是有重复的,变成键名之后这些重复的值便成了重复的键名,PHP 引擎将重复的键名删除,只保留最后一个。如:
代码如下 | 复制代码 |
$arr1 = array("age" => 30, "name" => '快乐园', "age" => 20); |
上面的代码写得简洁一些就是: $arr1 = array_flip(array_flip($arr1));
一些自定的函数
//删除数组中重复元素的函数
代码如下 | 复制代码 |
function delmember(&$array, $id) { $size = count($array); for($i = 0; $i { $array[$id + $i] = $array[$id + $i + 1]; } unset($array[$size - 1]); } |
//使用例子:
代码如下 | 复制代码 |
$output = array(1, 2, 2, 'www.bKjia.c0m', 5, 4, 4, 4, 2, 7, 5, 9, 10); function uniquearray($array) { // get unique elts as keys in assoc. array for ($i=0,$n=count($array, 1);$i $u_array[$array[$i]] = 1;
// copy keys only into another array reset($u_array, 1); for ($i=0,$n=count($u_array, 1);$i $unduplicated_array[] = key($u_array, 1); next($u_array, 1); } return $unduplicated_array; } |

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

AI Hentai Generator
Generate AI Hentai for free.

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



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
