php array_flip() and array_uniqure() remove duplicate elements from array_PHP tutorial

WBOY
Release: 2016-07-13 16:57:41
Original
1238 people have browsed it

There are several functions to delete duplicate elements from an array in PHP. One is array_unique() and the other is array_flip() and array_uniqure() functions, but the latter is several times more performant than the former, so I will only introduce the two. to remove duplicate array elements.

The method is as follows:

The code is as follows Copy code
 代码如下 复制代码

$arr = array(…………) ;// 假设有数组包含一万个元素,里面有重复的元素。

 
$arr = array_flip(array_flip($arr)); // 这样便可以删除重复元素。

$arr = array(…………) ;// Suppose there is an array containing ten thousand elements, with duplicate elements in it.


$arr = array_flip(array_flip($arr)); // This will remove duplicate elements.
 代码如下 复制代码

 
$arr1 = array("age" => 30, "name" => '快乐园');

 
$arr2 = array_flip($arr1); // $arr2 就是 array(30 => "age", '快乐园' => "name");


What is going on? Let’s look at the function of array_flip(): array_flip() is used to exchange the key and value of each element of an array, such as:

The code is as follows Copy code
 代码如下 复制代码

 
$arr1 = array("age" => 30, "name" => '快乐园', "age" => 20); "age" => 2 0将会取代 "age" => 30

 
$arr1 = array("name" => '快乐园', "age" => 45);

$arr1 = array("age" => 30, "name" => 'Happy Garden');

$arr2 = array_flip($arr1); // $arr2 is array(30 => "age", 'Happy Garden' => "name");

In a PHP array, different elements are allowed to take the same value, but the same key name is not allowed to be used by different elements, such as:

The code is as follows Copy code
 代码如下 复制代码

$arr1 = array("age" => 30, "name" => '快乐园', "age" => 20);

 
$arr1 = array_flip($arr1); // $arr1 变成了 array("快乐园" => "name", 20 => "age");

 
// 再把 $arr1 的键名与值还复:

 
$arr1 = array_flip($arr1);

$arr1 = array("age" => 30, "name" => 'Happy Garden', "age" => 20); "age" => 2 0 will replace "age" => 30

$arr1 = array("name" => 'Happy Garden', "age" => 45);

Here $arr1 and $arr2 are equal.

So, we can know why array_flip(array_flip($arr)) can delete duplicate elements in the array. First, the value in $arr will become a key name, because the value is repeated. After becoming a key name, these repeated values ​​will become duplicate key names. The PHP engine will delete the duplicate key names and only keep the last one. . Such as:
 代码如下 复制代码
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]);
}
<🎜> <🎜>
The code is as follows Copy code
<🎜>$arr1 = array("age" => 30, "name" => 'Happy Garden', "age" => 20); $arr1 = array_flip($arr1); // $arr1 becomes array("Happy Garden" => "name", 20 => "age"); //Restore the key name and value of $arr1: $arr1 = array_flip($arr1);
The above code can be written more concisely: $arr1 = array_flip(array_flip($arr1)); Some custom functions //Function to delete duplicate elements in an array
The code is as follows Copy code
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]); <🎜> } <🎜>

//使用例子:

 代码如下
代码如下 复制代码

$output = array(1, 2, 2, 'www.bKjia.c0m', 5, 4, 4, 4, 2, 7, 5, 9, 10);
delsame($output);
while(list($key, $value) = each($output))
{
echo "$key:$value"."
";
}
//方法二

function uniquearray($array)

{

// get unique elts as keys in assoc. array

for ($i=0,$n=count($array, 1);$i<$n;$i )

$u_array[$array[$i]] = 1;

// copy keys only into another array

reset($u_array, 1);

for ($i=0,$n=count($u_array, 1);$i<$n;$i ) {

$unduplicated_array[] = key($u_array, 1);

next($u_array, 1);

}

return $unduplicated_array;

}

复制代码
$output = array(1, 2, 2, 'www.bKjia.c0m', 5, 4, 4, 4, 2, 7, 5, 9, 10);
delsame($output);
while(list($key, $value) = each($output))
{
echo "$key:$value"."
";
}
//方法二

function uniquearray($array)

// get unique elts as keys in assoc. array for ($i=0,$n=count($array, 1);$i<$n;$i ) $u_array[$array[$i]] = 1;   // copy keys only into another array
reset($u_array, 1); for ($i=0,$n=count($u_array, 1);$i<$n;$i ) {
$unduplicated_array[] = key($u_array, 1); next($u_array, 1); } return $unduplicated_array; } http://www.bkjia.com/PHPjc/631495.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/631495.htmlTechArticle在php中删除数组重复元素的函数有几个,一个是array_unique()另外就是array_flip()与array_uniqure()函数,但后者比前者性能要高几倍了,所以我只介...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!