Jump to content

confusing loop question


dadamssg87

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.