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

1
  • 5
    Consider using array_count_values() to identify the duplicates.... why write your own function when PHP has one already built-in Commented Aug 22, 2017 at 8:39

6 Answers 6

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>';
   }
}
Sign up to request clarification or add additional context in comments.

Comments

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>";
}

?>

Comments

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
    }
}

Comments

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

Comments

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

   }

}

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.