0

I have a array that i want to check if has duplicates using PHP

$i=array('One','Two','Two','Three','Four','Five','Five','Six');

I was able to achieve it by using below function

  function array_not_unique($input) {
       $duplicates=array();
       $processed=array();
       foreach($input as $i) {
           if(in_array($i,$processed)) {
               $duplicates[]=$i;
           } else {
               $processed[]=$i;
           }
       }
       return $duplicates;

   }

I got below output

Array ( [0] => Two [1] => Five ) 

Now how can i display the previous arrays and mark values with duplicates referencing the array_not_unique function return values to a HTML table.

My goal is to display the duplicated values with red font color.

enter image description here

Rahul
  • 18,271
  • 7
  • 41
  • 60
KaoriYui
  • 912
  • 1
  • 13
  • 43
  • 5
    Consider using [array_count_values()](http://php.net/manual/en/function.array-count-values.php) to identify the duplicates.... why write your own function when PHP has one already built-in – Mark Baker Aug 22 '17 at 08:39

6 Answers6

5

Try this piece of code... simplest and shortest :)

$i=array('One','Two','Two','Three','Four','Five','Five','Six');

$arrayValueCounts  = array_count_values($i); 

foreach($i as $value){

    if($arrayValueCounts[$value]>1){

        echo '<span style="color:red">'.$value.'</span>';
    }
    else{

        echo '<span>'.$value.'</span>';
   }
}
Asad Khan
  • 116
  • 7
4

Here is optimized way that will print exact expected output.

<?php

$i=array('One','Two','Two','Three','Four','Five','Five','Six');

$dups = array_count_values($i);
print_r($dups);

foreach($i as $v)
{
    $colorStyle = ($dups[$v] > 1) ? 'style="color:red"' : '';
    echo "<span $colorStyle>$v</span>";
}

?>
Samir Selia
  • 7,007
  • 2
  • 11
  • 30
1

Try this,

function array_not_unique($input) {
       $duplicates=array();
       $processed=array();
       foreach($input as $key => $i) {
           if(in_array($i,$processed)) {
               $duplicates[$key]=$i; // fetching only duplicates here and its key and value
           } 
       }
       return $duplicates;
}
foreach($processed as $k => $V){
    if(!empty($duplicates[$k])){ // if duplicate found then red
        echo '<span style="color:red">'.$duplicates[$k].'</span>';
    }else{
        echo '<span>'.$duplicates[$k].'</span>'; // normal string
    }
}
Rahul
  • 18,271
  • 7
  • 41
  • 60
1

Like this :

PHP

function array_duplicate_css($input) {

   $output = $processed = array();

   foreach($input as $key => $i) {
       $output[$key]['value'] = $i;
       if(in_array($i, $processed)) {
           $output[$key]['class'] = 'duplicate';
       } else {
           $output[$key]['class'] = '';
           $processed[] = $i;
       }
   }

   return $output;
}

foreach(array_duplicate_css($input) as $row) {
    echo '<tr><td class="' . $row['class'] . '">' . $row['value'] . '</td></tr>';
}

CSS

.duplicate {
    color: #ff0000;
}
Meloman
  • 3,558
  • 3
  • 41
  • 51
0

Simple use array_count_values.

$array=array('One','Two','Two','Three','Four','Five','Five','Six');

$new_array= array_count_values($array);

foreach($new_array as $key=>$val){

  if($val>1){

     for($j=0;$j<$val;$j++){

         echo "<span style='color:red'>".$key."</span>";
     }

   }else{

     echo "<span>".$key."</span>";

   }

}
JYoThI
  • 11,977
  • 1
  • 11
  • 26
0

It can be done in several ways. This is just a one method. Step one maintain 2 arrays. Then store the duplicated indexes in one array. When you iterating use a if condition to check whether the index is available in the "duplicated indexes" array. If so add the neccessary css.

user3808887
  • 319
  • 2
  • 9