2

Hello I am looking for some help with removing an element from an array. My array $rooms holds events per rooms. Each room has its own array and each event has its own array within the room array. I loop through $rooms and display the room ID and within this loop I loop through the events for that room and display their contents. I would like to remove the array for the event that has been displayed so I don't have spend time comparing it when I repeat the same loop.

Below is an example of the process I am describing. I know it makes no sense to delete the element in this logic as I am using foreach, but within my application the logic of the function requires it..

$rooms

array(3) 
{ 
  [1]=> array(2) 
    { 
       ["rid"]=> string(1) "1" 
       ["events"]=> array(0) 
         { } 
    } 
  [2]=> array(2) 
    { 
       ["rid"]=> string(1) "2" 
       ["events"]=> array(0) 
         { } 
    } 
  [3]=> array(2) 
    { 
       ["rid"]=> string(1) "3" 
       ["events"]=> array(2) 
         { 
           [0]=> array(7) 
             { 
               ["lname"]=> string(20) "xxxxxxxxxxxxxxxxxxxx" 
             } 
           [1]=> array(7) 
             { 
               ["lname"]=> string(10) "yyyyyyyyyy" 
             } 
          }  
      } 
}

loop

foreach ($rooms as $room):
   echo $room['rid'];
   foreach ($room['events'] as $event):
       echo $event['lname'];

If you could tell me where within the foreach I should put the code and how should the code look like that would be really great. I think it should be right after echo $event['lname'], but I can't figure out how to locate the element that is displayed so I can unset it..

Thank you all for reading, looking forward to your replies.

1 Answer 1

1

You can simply unset the event after echoing it, but you'll need to reference it from $rooms, so you need to use the key's at each level. Try this:

foreach($rooms as $i => $room) {
    echo $room['rid'];
    foreach($room['events'] as $j => $event) {
        echo $event['lname'];
        unset($rooms[$i]['events'][$j]);
    }
}

You can see an example of it working here.

Sign up to request clarification or add additional context in comments.

4 Comments

Did that and then I have put echo '<div>'.var_dump($room).'</div>'; and the displayed vardump still has the events in it. I have the echo after the inside foreach is closed
@CreamWhippedAirplane I had a typo in the unset, I'd put $room instead of $rooms, which I've now corrected. Check the updated eval.in link and you can see me dumping it and it working.
Doesn't want to work for me :/ Can't tell what I am doing wrong. Especially since it works in you're eval. Could the problem be that in my application the inside foreach is enclosed in one more foreach that looks like this for($x=0; $x < 31; $x++): Basically what I am doing is generating a table where I use the rid as left heading and then generate 31 day cells using this foreach and on each day I loop through the $room['events'] and see if there is an event that matches the currently generated cell day...
Oh. I take it back! I must have been dumping the array in a wrong spot or something. I dumped $rooms after all the loops were closed and the events are gone. So thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.