在if子句为真的时候迭代一个结果

I have a condition that checks to see it the users id from the users table is the same as the user id that is attached to the post, and after that I want to attach a specific link for each post that meets that conditions with the actual posts data:

<?php if($post->user_id == $first):?>
                <?php foreach($code1 as $k=>$codes1): ?>
                <div class="imgSet">
                  <a href= "modify.php?1u=<?php echo $codes1->code;?>"> <!--hyperlink to edit text -->
                 <img src="img/settings.png"/> <!-- edit logo -->
                  <a/>
                </div>
                 <?php endforeach;?>
               <?php endif; ?>

Example: if $post->user_id== $first, my code will show all the value that $code gets from the database, I want to show one result at a time for each posts where the $post->user_id== $first.

To be more specific, if I have more posts with that meet the same condition in the if clause, I want to attach to that kink the code that is specific to that posts, but my program return for each condition meet all the result from the database.

You can use array_shift to get the first code from the codes array. All you have to assure is that you get the codes in the same order as the posts

<?php
foreach ($posts as $post) {
    if($post->user_id == $first) {
        $codes1 = array_shift($code1);
?>
<div class="imgSet">
    <a href="modify.php?1u=<?php echo $codes1->code;?>"> <!--hyperlink to edit text -->
        <img src="img/settings.png"/> <!-- edit logo -->
    <a/>
</div>
<?php
    }
}
?>