Home > Backend Development > PHP Tutorial > How to delete duplicate elements in an array in php, _PHP tutorial

How to delete duplicate elements in an array in php, _PHP tutorial

WBOY
Release: 2016-07-12 09:02:38
Original
778 people have browsed it

How to delete duplicate elements in an array in php,

Several php methods to delete array elements In many cases, our arrays will be duplicated, then we delete some duplicates in the array What to do with the content? These elements must remain unique, so we have to find ways to delete them. Here are several ways to delete duplicate array elements using traversal queries.
Method 1. Completely delete duplicate array instances-----Delete one element in the array

function array_remove_value(&$arr, $var){
foreach ($arr as $key => $value) {
if (is_array($value)) {
array_remove_value($arr[$key], $var);
} else {
$value = trim($value);
if ($value == $var) {
unset($arr[$key]);
} else {
$arr[$key] = $value;
}
}
}
}
Copy after login

$a is an array:

count($a); //得到4
unset($a[1]); //删除第二个元素
count($a); //得到3
echo $a[2]; //数组中仅有三个元素,本想得到最后一个元素,但却得到blue,
echo $a[1]; //无值
?>
Copy after login

That is to say, after deleting the elements in the array, the number of elements in the array (obtained with count()) has changed, but the array subscripts have not been rearranged, and the key before deleting the array must be used to operate accordingly. value.
Later, I adopted another method, which is actually not called a "method" at all. I used PHP4's ready-made function array_splice() .

count ($a); //得到4
array_splice($a,1,1); //删除第二个元素
count ($a); //得到3
echo $a[2]; //得到yellow
echo $a[1]; //得到blue
?>
Copy after login

Method 2, Function to delete duplicate elements in an array

function delmember(&$array, $id)
{
$size = count($array);
for($i = 0; $i <$size - $id - 1; $i ++)
{
$array[$id + $i] = $array[$id + $i + 1];
}
unset($array[$size - 1]);
}
Copy after login

Supplementary example:

Method 1. php has a built-in function array_unique that can be used to delete duplicate values ​​in an array

  • array_unique -- Remove duplicate values ​​from an array
  • array_unique description
  • array array_unique (array array)
  • array_unique() accepts an array as input and returns a new array with no duplicate values

Note that the key name remains unchanged. array_unique() sorts the values ​​first as strings, then retains only the first encountered key for each value, and then ignores all subsequent keys. This does not mean that the first occurrence of the same value in an unsorted array will be preserved.
Note: Two elements are considered the same if and only if (string) $elem1 === (string) $elem2. That is, when the expressions of the strings are the same.
The first unit will be retained.
Example: array_unique()

<&#63;php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
&#63;> 
Copy after login

The above example will output:

Array
(
 [a] => green
 [0] => red
 [1] => blue
) 
Copy after login

Method 2, array_flip achieves deduplication effect

Another method is to use PHP’s array_flip function to indirectly achieve the deduplication effect
array_flip is a function that reverses the keys and values ​​of an array. It has a feature that if two values ​​in the array are the same, the last key and value will be retained after the reversal. We use this feature to indirectly implement the array. Remove duplicates.

<&#63;php
$arr = array("a"=>"a1","b"=>'b1',"c"=>"a2","d"=>"a1");
$arr1 = array_flip($arr);
print_r($arr1);//先反转一次,去掉重复值,输出Array ( [a1] => d [b1] => b [a2] => c )
$arr2 = array_flip($arr);
print_r($arr2);//再反转回来,得到去重后的数组,输出Array ( [a1] => d [b1] => b [a2] => c )
$arr3 = array_unique($arr);
print_r($arr3);//利用php的array_unique函数去重,输出Array ( [a] => a1 [b] => b1 [c] => a2 )
&#63;>
Copy after login

The difference between the two methods is that using array_flip will get the last key and value of the repeated element, and using array_unique will get it It is the first key and value of two repeated elements.

I hope this article will help you learn PHP programming and solve the problem of repeated elements in arrays.

Articles you may be interested in:

  • PHP method to sort a two-dimensional array by specified key value
  • PHP submission post array parameter example analysis
  • Summary of PHP array function array_key_exists()
  • PHP multi-dimensional array traversal method (2 implementation methods)
  • Summary of some commonly used add, delete and insert operation functions for arrays in PHP
  • Detailed explanation of PHP’s definition of arrays and how to create arrays
  • Realizing random merging and sorting of arrays (original sorting) based on PHP
  • How to implement traversing multi-dimensional arrays in PHP
  • Simple implementation method of converting multi-dimensional array to one-dimensional array in PHP

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1084568.htmlTechArticlephp methods to delete duplicate elements in arrays, several php methods to delete array elements. In many cases our arrays will Duplication occurs, how about we delete some duplicate contents in the array...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template