Solved

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

Posted on 2013-11-13
6
1,979 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 110

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 110

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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 17

Author Closing Comment

by:jrm213jrm213
ID: 39645059
Thanks!
0
 
LVL 110

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 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