Jump to content

Archived

This topic is now archived and is closed to further replies.

benjam

unset($array[$i]) not working

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?

Share this post


Link to post
Share on other sites
i think the problem lies in the condition. maybe $parties[$i]['guests'] == $parties[$i]['total'] never evaluates to true, thus unset is not even executed.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

×

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.