?
Solved

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

Posted on 2013-11-13
6
Medium Priority
?
2,017 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 111

Assisted Solution

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

Accepted Solution

by:
Ray Paseur earned 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 17

Author Closing Comment

by:jrm213jrm213
ID: 39645059
Thanks!
0
 
LVL 111

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

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
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…
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.
Suggested Courses

752 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