$v1){$r1[$v1]=$k1;}"; 3. Use the foreach statement to traverse the reversed array and assign the keys and values ​​to another empty array as values ​​and keys , the syntax is "foreach($r1 as $k2=>$v2){$r2[$v2]=$k2;}"."/> $v1){$r1[$v1]=$k1;}"; 3. Use the foreach statement to traverse the reversed array and assign the keys and values ​​to another empty array as values ​​and keys , the syntax is "foreach($r1 as $k2=>$v2){$r2[$v2]=$k2;}".">

Home  >  Article  >  Backend Development  >  How to implement array deduplication in php without using functions

How to implement array deduplication in php without using functions

青灯夜游
青灯夜游Original
2022-09-21 18:58:321699browse

Implementation steps: 1. Define 2 empty arrays to store the twice reversed keys and values; 2. Use the foreach statement to traverse the original array and assign the original array keys and values ​​to an empty array as Values ​​and keys, the syntax is "foreach($arr as $k1=>$v1){$r1[$v1]=$k1;}"; 3. Use the foreach statement to traverse the reversed array and assign the keys and values ​​to another An empty array is used as the value and key, and the syntax is "foreach($r1 as $k2=>$v2){$r2[$v2]=$k2;}".

How to implement array deduplication in php without using functions

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

In php, the key name of the array (key ) is unique and will not exist repeatedly; even if two identical key names are declared, the key name declared later will overwrite the previous key name.

Using the non-repeatable feature of PHP array key names, you can remove duplicate values ​​from the array.

Implementation method: Reverse the key name and key value position of the array twice.

Implementation steps:

#Step 1: Define 2 empty arrays to store the key names and keys reversed twice Value

$res1=[];
$res2=[];

Step 2: Use the foreach statement to traverse the original array and assign the original array key name and key value to an empty array as the key value and key name

foreach ($array as $k1 => $v1){  //在每次循环中会将当前数组的值赋给 $v1,键名赋给 $k1
    $res1[$v1]=$k1;
}

You will get an array with reversed key names and key values

Step 3: Use the foreach statement to traverse the reversed array, and assign the key names and key values ​​of the reversed array to another empty array as a key Values ​​and key names

foreach ($res1 as $k2 => $v2){  
    $res2[$v2]=$k2;
}

Implementation code

<?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 implement array deduplication in php without using functions

Recommended learning: "PHP Video Tutorial

The above is the detailed content of How to implement array deduplication in php without using functions. 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