Home > Backend Development > PHP Tutorial > 请教大神,php怎么数组去掉重复值?

请教大神,php怎么数组去掉重复值?

WBOY
Release: 2016-06-23 13:58:50
Original
749 people have browsed it

举例

$array=array(1,2,3,3)

经过程序处理,一旦知道又重复数据,就删除,

最后结果我需要array(1,2),因为3重复了


回复讨论(解决方案)

数组值3也不要吗

数组值3也不要吗


是啊

array_unique()啊

$arr = array(1,2,3,3);
$str = implode('',$arr);
$arrres = array_unique($arr);
foreach($arrres as $k=>$v)
{
$num = substr_count($str,$v);
if($num == 1)
{
$res[] = $v;
}
}
print_r($res);

$array = array(1,2,3,3);$t = array_diff($array,  array_keys(    array_filter(      array_count_values($array), function($v) { return $v>1;})    )  );print_r($t);
Copy after login
Copy after login
Copy after login
Array
(
[0] => 1
[1] => 2
)

$arr = array(1,2,3,3);
$str = implode('',$arr);
$arrres = array_unique($arr);
foreach($arrres as $k=>$v)
{
$num = substr_count($str,$v);
if($num == 1)
{
$res[] = $v;
}
}
print_r($res);

谢谢你提供的思路,但是代码好像可以简单点: $arr = array(1,2,3,3);
$str = implode('',$arr);
foreach($arr as $k=>$v)
{
$num = substr_count($str,$v);
if($num == 1){
$res[] = $v;
}
}
print_r($res);
?>,

$array = array(1,2,3,3);$t = array_diff($array,  array_keys(    array_filter(      array_count_values($array), function($v) { return $v>1;})    )  );print_r($t);
Copy after login
Copy after login
Copy after login
Array
(
[0] => 1
[1] => 2
)

和我想的一样

$array = array(1,2,3,3);$t = array_diff($array,  array_keys(    array_filter(      array_count_values($array), function($v) { return $v>1;})    )  );print_r($t);
Copy after login
Copy after login
Copy after login
Array
(
[0] => 1
[1] => 2
)



版主牛B

稍稍简化一下

$array = array(1,2,3,3);$t = array_diff($array,  array_keys(    array_diff(      array_count_values($array), array(1))    )  );print_r($t);
Copy after login

$array=array(1,2,3,3);
$a=array_count_values($array);
$item=array();
foreach($a as $k=>$v) if($v==1) $item[]=$k;
print_r($item);

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