0

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);

}

?>

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

Zimo
  • 1
  • 1

3 Answers3

4

Use the array_unique($array) function.

It removes duplicate values of an array.

Please check this for more information :

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

Lilian Barraud
  • 337
  • 1
  • 11
1

If $cache is multidimensionnal then array_unique will not work. You may want to use :

$input = array_map("unserialize", array_unique(array_map("serialize", $input)));

See : How to remove duplicate values from a multi-dimensional array in PHP

Gourgandine
  • 189
  • 6
0

Or let the database handle the removing the duplication with DISTINCT

SELECT 
  DISTINCT
     id 
   , postdata
FROM 
 client 
WHERE 
 status = 1 
ORDER BY
 id ASC
Raymond Nijland
  • 11,488
  • 2
  • 22
  • 34
  • postdata columns content mixed data not only number value – Zimo Mar 21 '18 at 13:39
  • So? DISTINCT also works on mixed data not only on numbers – Raymond Nijland Mar 21 '18 at 13:43
  • postdata columns content for example : id 1 postdata : xxx 123 | id 2 postdata : hello 123 | id 3 postdata : hey 123 | id 4 postdata : xxx 000 | i need get only 2 row 123 and 000 whitout duplicate – Zimo Mar 21 '18 at 13:49