Solved

PHP - Delete Object from array while in a foreach loop iterating that array

Posted on 2013-11-13
6
1,850 Views
Last Modified: 2013-11-13
Hi,

I am hoping this is a simple question with a simple answer that I am just not seeing.

I have an array of existing people objects, and an array of people objects that need to be removed from the list of existing people.

foreach($entity->removals as $removal){
    foreach($entity->existing_list as $existing){
        if($removal->get_id() == $existing->get_id()){
                //is there a simple way to remove $existing from $entity->existing_list
        }
    }
}

Open in new window

0
Comment
Question by:jrm213jrm213
  • 3
  • 3
6 Comments
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 39645022
You can use an array key in foreach(). Can you show us a bit of test data?
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39645026
Untested but possibly correct...

foreach($entity->removals as $removal){
    foreach($entity->existing_list as $key => $existing){
        if($removal->get_id() == $existing->get_id()){
            unset($entity->existing_list[$key]);
        }
    }
}

Open in new window

0
 
LVL 17

Author Comment

by:jrm213jrm213
ID: 39645053
interesting so changing the structure of the foreach like that makes $key into the index of the array while putting the value into $existing. I didn't realize it worked that way. Thanks!
0
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
LVL 17

Author Closing Comment

by:jrm213jrm213
ID: 39645059
Thanks!
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39645153
Thanks for the points.  You might find some value in this article...
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12310-PHP-Variables-and-References.html

Best regards, ~Ray
0
 
LVL 17

Author Comment

by:jrm213jrm213
ID: 39645172
yeah in the past when I wanted to modify the object for the current iteration I just made the for loop like this

foreach($list as &$list_item){
}

Open in new window


which would make list_item as a reference instead of a value, but that doesn't help when you need to remove $list_item from the list, it just lets you modify that item in the list.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question