How to avoid duplicates in nested foreach loop php

Mohammed Sadakath Ali Aamiri

Why are values are getting printed multiple times (see images below). I need to print them only once.

<?php foreach($patchData3 as $tes3){?> 
    <?php foreach($patchData1 as $tes){?>
        <tr class="<?php if($tes->PATCH == $tes3->PATCH) {echo "red_color"; } ?>"> 
            <td><?php echo $tes->HOSTNAME;?></td>
            <td><?php echo $tes->VERSION;?></td> 
            <td><?php echo $tes->PATCH;?></td> <!--bgcolor="#FF0000"-->
        </tr>   
    <?php } ?>
<?php }?>

enter image description here
enter image description here

Geordy James
<?php foreach($patchData1 as $tes){ ?>          
    <tr class="<?php if(checkfunction($tes->PATCH,$patchData3) == TRUE) { echo "red_color"; } ?>"> 
        <td><?php echo $tes->HOSTNAME;?></td>
        <td><?php echo $tes->VERSION;?></td> 
        <td><?php echo $tes->PATCH;?></td> <!--bgcolor="#FF0000"-->
    </tr>   
<?php } ?>

<?php 
    function checkfunction($patch,$patchData3){
        foreach($patchData3 as $tes3){
            if($patch == $tes3->PATCH){
                return true;
            }   
        }
    }
?>

I used a function to overcome the duplication. Please comment if it does not work.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP nested foreach generating duplicates

From Dev

How to avoid nested forEach calls?

From Dev

PHP nested foreach loop on an array

From Dev

PHP foreach loop in nested array

From Dev

PHP nested foreach loop on an array

From Dev

Ignore Duplicates in Foreach loop

From Dev

Ignore Duplicates in Foreach loop

From Dev

PHP - Unexpected Behavior in Nested foreach Loop

From Dev

PHP functions nested foreach loop does not work

From Dev

Matched results in nested foreach loop - PHP

From Java

How to use nested foreach loop in jQuery?

From Dev

How to use ProgressBar inside nested foreach loop?

From Dev

How to set configuration in nested foreach loop container

From Dev

PHP - How to limit a foreach loop?

From Dev

How to avoid duplicates in arangoDB

From Dev

nested arrays how to foreach it proper using smarty (loop in loop)

From Dev

How to configure foreach loop properly to avoid index out of range error?

From Dev

nested foreach loop with IF statement

From Dev

Nested increasing foreach loop

From Dev

Foreach Nested Loop No Repetition

From Dev

Nested foreach loop with pointers?

From Dev

PHP convert flat to nested multidimensional array in foreach loop

From Dev

How to force a field to avoid duplicates?

From Dev

How to avoid inserting duplicates into MySQL?

From Dev

How to avoid duplicates in XQuery results?

From Dev

How to avoid duplicates in INSERT INTO wordpress

From Dev

How to force a field to avoid duplicates?

From Dev

How to avoid pushing duplicates into array

From Dev

Avoid updating nested lists in for loop