PHP中如何去除重复值 [英] How remove duplicate value in PHP

查看:54
本文介绍了PHP中如何去除重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据存储在 $cache[] 中,里面有一些数字.如何删除打印输出中的重复值?:

I have a some data stored in $cache[], there are some number in it. How do I remove duplicate values in a printed output? :

<?

#mysql connect
mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_select_db($name) or die(mysql_error());

function get_numerics ($str) {
    preg_match_all('/\d+/', $str, $matches);
    return $matches[0];
}


function validatecard($number) {
//preg_match_all('/^[6-9][0-9]{9}$/', $number, $found);
preg_match_all('/^[0-9]{4}$/',$number, $found);

//print_r($found);
return $found[0];

 }


$query = mysql_query("SELECT * FROM client WHERE status = 1 ORDER BY id") 
         or die(mysql_error());

while ($raw = mysql_fetch_array($query)) 

{

$name = $raw["postdata"];
$id = $raw["id"];
$cache = [];


  for($i=0;$i<=count(get_numerics($name));$i++){
    $ccv = get_numerics($name);
    $num = $ccv[$i];
    if (is_numeric($num)) {
    $cc = validatecard($num);
    if (!in_array($cc, $cache)){
        $cache[] = $cc;
}   
}
}



print_r($cache);

}

?>

我使用了一些像 :array unique 这样的函数并将其转换为 json 或序列化......但不起作用.

i have use some function like :array unique and convert it to json or serialize... and not work.

推荐答案

使用 array_unique($array) 函数.

它删除数组的重复值.

请检查此以获取更多信息:

Please check this for more information :

http://php.net/manual/en/function.array-unique.php

这篇关于PHP中如何去除重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆