请问大神,php如何数组去掉重复值?

WBOY
Release: 2016-06-13 11:54:47
Original
895 people have browsed it

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

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

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

最后结果我需要array(1,2),因为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);<br />$t = array_diff($array,<br />  array_keys(<br />    array_filter(<br />      array_count_values($array), function($v) { return $v>1;})<br />    )<br />  );<br />print_r($t);
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);<br />$t = array_diff($array,<br />  array_keys(<br />    array_filter(<br />      array_count_values($array), function($v) { return $v>1;})<br />    )<br />  );<br />print_r($t);
Copy after login
Copy after login
Array
(
[0] => 1
[1] => 2
)
和我想的一样
------解决方案--------------------
稍稍简化一下
$array = array(1,2,3,3);<br />$t = array_diff($array,<br />  array_keys(<br />    array_diff(<br />      array_count_values($array), array(1))<br />    )<br />  );<br />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);

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!