benjam Posted May 15, 2006 Share Posted May 15, 2006 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 partiesfor ($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? Quote Link to comment https://forums.phpfreaks.com/topic/9731-unsetarrayi-not-working/ Share on other sites More sharing options...
ryanlwh Posted May 15, 2006 Share Posted May 15, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/9731-unsetarrayi-not-working/#findComment-36062 Share on other sites More sharing options...
benjam Posted May 23, 2006 Author Share Posted May 23, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/9731-unsetarrayi-not-working/#findComment-38382 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.