1

I am trying to display an array of data produced from an sql query. this works fine. However I now wish not to display 1 item where the id attribute = a value. code:

<?php foreach($sheet_list as $key => $sheet) : ?> 
        <?php if($sheet['id'] == $selected1){unset ($sheet_list[$key]);}?>
        <tr>
            <td><?=$sheet['title'];?></td>
            <td><?php echo date('d F Y', strtotime($sheet['startDate'])); ?></td>
            <td><?php echo date('d F Y', strtotime($sheet['endDate'])); ?></td>
            <td><input type="radio" name="selected2" id="selected2" value="<?=$sheet['id'];?>" <?php 
            if ($key == 0) echo ' checked ';
            ?>/></td>
        </tr>
        <?php endforeach; ?>

so this is producing a table which is fine, however it is in the foreach loop where i want NOT to display 1 array item and all its attributes where the "id" field is = to the variable $selected1 I have seen lots of questions posted here, however many for 1 or two dimensional arrays. Please help.

2 Answers 2

1

You're close, you should be using continue; instead of unset():

<?php if($sheet['id'] == $selected1){ continue; } ?>

continuePHP Docs makes the loop skip to the next iteration, effectively skipping the current iteration, which is what you want to do.

The benefit here is that you never modify for you $sheet_list array, unlike with unset() where you'll remove that item from the array.

Sign up to request clarification or add additional context in comments.

Comments

0

If you really want to unset $sheet_list[$selected1] why don't you just do it before you enter the loop?

If you just want to skip output for this item, then use continue as suggested by nickb.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.