$v1){$r1[$v1]=$k1;}foreach($r1 as $k2=>$v2){$r2[$v2]=$k2;}"."/> $v1){$r1[$v1]=$k1;}foreach($r1 as $k2=>$v2){$r2[$v2]=$k2;}".">

Home  >  Article  >  Backend Development  >  How to remove identical values ​​from an array in php

How to remove identical values ​​from an array in php

青灯夜游
青灯夜游Original
2022-09-28 18:21:053663browse

Three methods: 1. Use array_unique, the syntax "array_unique(array)"; 2. Use array_flip(), the syntax "array_flip(array_flip(array))"; 3. Use foreach and two empty arrays , the syntax "foreach($arr as $k1=>$v1){$r1[$v1]=$k1;}foreach($r1 as $k2=>$v2){$r2[$v2]=$ k2;}".

How to remove identical values ​​from an array in php

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

Removing the same values ​​​​in the array is to remove the array Duplicate values ​​in . There are many ways to remove duplicates from PHP arrays. Let’s introduce them to you below.

Method 1: Use the deduplication function array_unique()

array_unique() function is used to remove duplicate values ​​in the 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.

array_unique(array,sortingtype)
Parameters Description
array Required . Specifies an array.
sortingtype Optional. Specifies the sorting type. Possible values:
  • SORT_STRING - Default. Treat each item as a string.
  • SORT_REGULAR - Sort each item in regular order (Standard ASCII, does not change type).
  • SORT_NUMERIC - Treat each item as a number.
  • SORT_LOCALE_STRING - Treat each item as a string, based on the current locale (can be changed via setlocale()).

Return value: Returns the filtered array.

Example: Remove identical values

<?php
$arr1 =array("a"=>"Cat",2,"b"=>"Dog","c"=>"Cat",2,3,4,3);
var_dump($arr1);
$arr2=array_unique($arr1);
var_dump($arr2);
?>

How to remove identical values ​​from an array in php

##Method 2: Using the array_flip() function

array_flip() function is used to reverse/exchange the key names in the array and the corresponding associated key values.

The key name of the array is unique and will not exist repeatedly.

Using this feature, if two values ​​in the array are the same, the last key and value will be retained after reversing the array, which can indirectly achieve deduplication of the array.

<?php
$arr =array("a"=>"Cat",2,"b"=>"Dog","c"=>"Cat",2,3,4,3);
var_dump($arr);
$arr1 = array_flip($arr);//先反转一次,去掉重复值
var_dump($arr1);
$arr2 = array_flip($arr1);//再反转回来,得到去重后的数组
var_dump($arr2);
?>

How to remove identical values ​​from an array in php

The difference between method 1 and method 2:

Use array_flip() to get the last key and value of the repeated element , use array_unique to get the first key and value in the repeated element.

Method 3: Use the foreach statement and two empty arrays

Or use the unique feature of the key name, use the foreach statement and two empty arrays Array to reverse the key name and key value position of the array twice.

<?php 
header("content-type:text/html;charset=utf-8");
function f($arr){
	var_dump($arr);
	$res1=[];
	$res2=[];
	foreach($arr as $k1=>$v1){
		$res1[$v1]=$k1;
	}
	foreach ($res1 as $k2 => $v2){  
	    $res2[$v2]=$k2;
	}
	echo "去重后的数组:";
	var_dump($res2);
}

$arr=array(1,2,3,4,5,4,3,2,1,0);
f($arr);
?>

How to remove identical values ​​from an array in php

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to remove identical values ​​from 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