Home >Backend Development >PHP Problem >How to delete duplicate element values ​​in an array in php

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

青灯夜游
青灯夜游Original
2021-03-17 17:20:112648browse

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