Jump to content


Photo

unset($array[$i]) not working


  • Please log in to reply
2 replies to this topic

#1 benjam

benjam
  • Members
  • PipPipPip
  • Advanced Member
  • 43 posts
  • LocationSalt Lake City, UT

Posted 15 May 2006 - 07:33 PM

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:
// 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
}

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

Why is unset($parties[$i]) not working?

Windows 7
MySQL: 5.1.36-community
PHP: 5.3.2 VC9
Apache 2.2 (apachelounge VC9)

#2 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 15 May 2006 - 07:37 PM

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.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#3 benjam

benjam
  • Members
  • PipPipPip
  • Advanced Member
  • 43 posts
  • LocationSalt Lake City, UT

Posted 23 May 2006 - 08:52 PM

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?
Windows 7
MySQL: 5.1.36-community
PHP: 5.3.2 VC9
Apache 2.2 (apachelounge VC9)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users