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.

2
  • array_unique(array_values($cache)); Commented Mar 21, 2018 at 13:06
  • I tried to apply it on my code is not working Commented Mar 21, 2018 at 13:44

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

not work for me in the following code
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

1 Comment

I tried to apply it on my code is not working
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

3 Comments

postdata columns content mixed data not only number value
So? DISTINCT also works on mixed data not only on numbers
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.