search
HomeBackend DevelopmentPHP ProblemHow to delete duplicate element values ​​in an array in php

php method to delete duplicate values ​​in an array: 1. Use array_unique() function, syntax "array_unique(array)". 2. First use the array_flip() function to reverse the keys and values ​​of the array and remove duplicate values; then use array_flip() to reverse the array back.

How to delete duplicate element values ​​in an array in php

The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer

php deletes duplicates in the array Element value

Method 1: Directly use the array_unique() function

<?php
$arr1 = array("a" => "green", "b" => "red", "c" => "green", "d" => "blue","e" =>  "red");
var_dump($arr1);
$arr2 = array_unique($arr1);
var_dump($arr2);
?>

Output:

array (size=5)
  &#39;a&#39; => string &#39;green&#39; (length=5)
  &#39;b&#39; => string &#39;red&#39; (length=3)
  &#39;c&#39; => string &#39;green&#39; (length=5)
  &#39;d&#39; => string &#39;blue&#39; (length=4)
  &#39;e&#39; => string &#39;red&#39; (length=3)
  
array (size=3)
  &#39;a&#39; => string &#39;green&#39; (length=5)
  &#39;b&#39; => string &#39;red&#39; (length=3)
  &#39;d&#39; => string &#39;blue&#39; (length=4)

Instructions:

array_unique() function is used to remove duplicate values ​​from an array. If two or more array values ​​are the same, only the first value is retained and the other values ​​are removed.

Note: The retained array will retain the key type of the first array item.

Syntax

array_unique(array)

How to delete duplicate element values ​​in an array in php

Method 2: Use the array_flip() function twice

<?php
header("content-type:text/html;charset=utf-8");
$arr = array("a" => "green", "b" => "red", "c" => "green", "d" => "blue","e" =>  "red");
// 输出原始数组
echo "原始数组 :";
var_dump($arr);
// 通过使用翻转键和值移除重复值
$arr = array_flip($arr);
// 通过再次翻转键和值来恢复数组元素
$arr = array_flip($arr);
// 重新排序数组键
$arr = array_values($arr);
// 输出更新后的数组
echo "更新数组 :";
var_dump($arr);
?>

Output:

原始数组 :
array (size=5)
  &#39;a&#39; => string &#39;green&#39; (length=5)
  &#39;b&#39; => string &#39;red&#39; (length=3)
  &#39;c&#39; => string &#39;green&#39; (length=5)
  &#39;d&#39; => string &#39;blue&#39; (length=4)
  &#39;e&#39; => string &#39;red&#39; (length=3)
  
更新数组 :
array (size=3)
  0 => string &#39;green&#39; (length=5)
  1 => string &#39;red&#39; (length=3)
  2 => string &#39;blue&#39; (length=4)

Explanation:

array_flip() is a function that reverses the keys and values ​​of an array. It has a characteristic that if two values ​​​​in the array are the same, then after the reversal Keep the last key and value, and use this feature to indirectly achieve duplication of the array.

Grammar

array_flip(array);

How to delete duplicate element values ​​in an array in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to delete duplicate element values ​​in an array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement
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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)