Jump to content

unset($array[$i]) not working


benjam

Recommended Posts

I have a script that outputs incomplete parties by checking two elements in an array (total and (current) guests) against each other, and if they are equal, it deletes that part of the array and then afterwards, outputs the rest of the array (the incomplete parties)

Here is some code:
[code]
// remove the complete parties
for ($i = 0; $i < count($parties); $i++)
{
    if ($parties[$i]['guests'] == $parties[$i]['total'])
    {
        unset($parties[$i]);
    }
}

foreach ($parties as $party)
{
    // some html output code for $party
}
[/code]

But it outputs all the parties, not just the incomplete ones.

Why is unset($parties[$i]) not working?
Link to comment
Share on other sites

  • 2 weeks later...
No, I have purposely entered test cases that ensure that the condition is met.

I even have output right before the condition which tells me the condition was met.

I ran a few more tests, and it seems that when I use $i <= count() instead of $i < count(), it works and gets all of the entries, before it was missing one.

And I just figured out why... because as the code goes through, count() changes. So I'll need to set a seperate var equal to count before running the for statement.

Could this unset probelm also have something to do with the GLOBAL var?
Because when I use unset($parties[$i]); then unset($GLOBALS['parties'][$i]) it works.

Or is it mostly due to the count issue previously mentioned?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.