How to remove duplicate values from a php array: First create a PHP sample file; then define an array; and finally use the "array_unique($arr);" method to remove duplicates from the elements in the array.
# Recommended: "PHP Video Tutorial》
1. Use the array_unique method to deduplicate
To deduplicate array elements, we generally use the array_unique method, use this Method can remove duplicate elements from an array.<?php $arr = array(1,1,2,3,3,3,4,4,5,6,6,7,8,8,9,9,9); $arr = array_unique($arr); $arr = array_values($arr); print_r($arr);?>Output:
Array( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9)After deduplication, the key values will be out of order. You can use array_values to reorder the key values.
2. Use array_unique method to remove duplicates for efficiency <?php
$arr = array();
// 创建100000个随机元素的数组
for($i=0; $i<100000; $i++){
$arr[] = mt_rand(1,99);
}
// 记录开始时间
$starttime = getMicrotime();
// 去重
$arr = array_unique($arr);
// 记录结束时间
$endtime = getMicrotime();
$arr = array_values($arr);
echo 'unique count:'.count($arr).'<br>';
echo 'run time:'.(float)(($endtime-$starttime)*1000).'ms<br>';
echo 'use memory:'.getUseMemory();
/**
* 获取使用内存
* @return float
*/
function getUseMemory(){
$use_memory = round(memory_get_usage(true)/1024,2).'kb';
return $use_memory;
}
/**
* 获取microtime
* @return float
*/
function getMicrotime(){
list($usec, $sec) = explode(' ', microtime());
return (float)$usec + (float)$sec;
}
?>
unique count:99 run time:653.39303016663ms
use memory:5120kb
array_unique method to remove duplicates. The running time takes about 650ms and the memory usage is about 5m
3. Faster array deduplication method
php has a key-value exchange method array_flip. We can use this method to deduplicate. Because of key-value exchange, the original duplicate values will become for the same key. Then perform a key-value exchange, and exchange the keys and values back to complete the deduplication.
<?php $arr = array(); // 创建100000个随机元素的数组 for($i=0; $i<100000; $i++){ $arr[] = mt_rand(1,99); } // 记录开始时间 $starttime = getMicrotime(); // 使用键值互换去重 $arr = array_flip($arr); $arr = array_flip($arr); // 记录结束时间 $endtime = getMicrotime(); $arr = array_values($arr); echo 'unique count:'.count($arr).'<br>'; echo 'run time:'.(float)(($endtime-$starttime)*1000).'ms<br>'; echo 'use memory:'.getUseMemory(); /** * 获取使用内存 * @return float */ function getUseMemory(){ $use_memory = round(memory_get_usage(true)/1024,2).'kb'; return $use_memory; } /** * 获取microtime * @return float */ function getMicrotime(){ list($usec, $sec) = explode(' ', microtime()); return (float)$usec + (float)$sec; } ?>unique count:99
run time:12.840032577515ms
use memory:768kb
array_flip method to remove duplicates, the running time takes about 18ms, the memory usage is about 2m
. Therefore, using thearray_flip method to remove duplicates takes longer than using the array_unique method. The running time is 98%, memory usage reduced4/5;
The above is the detailed content of How to remove duplicates from php array. For more information, please follow other related articles on the PHP Chinese website!

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

Atom editor mac version download
The most popular open source editor

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
