dadamssg87 Posted October 18, 2011 Share Posted October 18, 2011 I was wondering if you loop through an array and in the foreach() you specify an if statement that unsets certain items from the array, if those get unset from the original loop. Probably doesn't make much sense so heres an example <?php //say there are 3 items in the $parent array //say there are 10 items in the $items array //say only the second item in the $items array value_one equals "abcd" foreach($parent as $id => $value) { foreach($items as $key => $value) { if($items[$key]['value_one'] == "abcd") { unset($items[$key]); } } } ?> So on the first item in the parent array, all 10 items would be cycled through. On the second second item of the parent array, all 10 items would be cycled through and the one item that has value_one == "abcd" would be unset. So on the third item of the parent array, am i correct in assuming only 9 items would be cycled through because there would be only 9 items in the $items array? Quote Link to comment https://forums.phpfreaks.com/topic/249297-confusing-loop-question/ Share on other sites More sharing options...
Psycho Posted October 18, 2011 Share Posted October 18, 2011 No. The number of child elements for one parent has nothing to do with the number of child elements for another parent. So, when you unset one of the values under the second parent it has no effect on the values under the third parent. Is this just a hypothetical question or is there something you are trying to accomplish? If the latter, give an example of the array you are dealing with and an explanation of the logic you need to unset values. Quote Link to comment https://forums.phpfreaks.com/topic/249297-confusing-loop-question/#findComment-1280116 Share on other sites More sharing options...
dadamssg87 Posted October 18, 2011 Author Share Posted October 18, 2011 eh, not really. Just trying to optimize where ever possible. I thought if i did the unsetting it would make it run faster, even if it is an insignificant amount. Thanks for the answer! Quote Link to comment https://forums.phpfreaks.com/topic/249297-confusing-loop-question/#findComment-1280198 Share on other sites More sharing options...
Psycho Posted October 18, 2011 Share Posted October 18, 2011 Well, looking at one of your earlier posts (http://www.phpfreaks.com/forums/index.php?topic=345762.msg1632361#msg1632361) it looks like the real problem is how you are storing the data. You should not store multiple pieces of data into a single field. Instead of taking some time to learn the proper way to work with this data (associated tables) you are building an unsustainable solution. Quote Link to comment https://forums.phpfreaks.com/topic/249297-confusing-loop-question/#findComment-1280205 Share on other sites More sharing options...
dadamssg87 Posted October 18, 2011 Author Share Posted October 18, 2011 haha yeah. That was the only instance that i was storing data stupidly and i've fixed that. But that doesn't have anything to do with this question Quote Link to comment https://forums.phpfreaks.com/topic/249297-confusing-loop-question/#findComment-1280208 Share on other sites More sharing options...
ManiacDan Posted October 18, 2011 Share Posted October 18, 2011 I wrote an article on this subject. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/249297-confusing-loop-question/#findComment-1280236 Share on other sites More sharing options...
dadamssg87 Posted October 18, 2011 Author Share Posted October 18, 2011 wow, never knew looping could be so complicated haha. Thanks for the write up! Quote Link to comment https://forums.phpfreaks.com/topic/249297-confusing-loop-question/#findComment-1280260 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.