Your Location is: Home > Php

Find and use duplicate element in array php mysql

From: Burma View: 1627 Garrison 
 php

Question

I have merged two arrays with a total of 6 elements

$merge = (array_merge($a1,$a2));

I want to find the duplicate values and use this information to call and attach a specific image to those values within a table

foreach($merge as $data => $data_array){
   $search_data = array_search($data_array, $merge);
   if ($data != $search_data) {
            echo '<table>';
            echo '<thead>';
            echo '<tr>';
            echo '<td ><img src="img/youAreNotDuplicated.png" width="85px" height="85px"></img></td>';
            echo '</tr>';
            echo '</thead>';
            echo '</table>';
        }else{
            echo '<table>';
            echo '<thead>';
            echo '<tr>';
            echo '<td ><img src="img/youAreDuplicated.png" width="85px" height="85px"></img></td>';
            echo '</tr>';
            echo '</thead>';
            echo '</table>';
        }
}

The problem is I end up with 7 images instead of 6. The duplicated value is added to the table. How do I remove the duplicated value from my table without altering the array.

Thanks

Best answer

You can use array_diff, but this way will be more faster:

$cnt_array = array_count_values($merge)
foreach($cnt_array as $key=>$val){
   if($val == 1){
            echo '<table>';
            echo '<thead>';
            echo '<tr>';
            echo '<td ><img src="img/youAreNotDuplicated.png" width="85px" height="85px"></img></td>';
            echo '</tr>';
            echo '</thead>';
            echo '</table>';
   }
   else{
            echo '<table>';
            echo '<thead>';
            echo '<tr>';
            echo '<td ><img src="img/youAreDuplicated.png" width="85px" height="85px"></img></td>';
            echo '</tr>';
            echo '</thead>';
            echo '</table>';
   }
}