Jump to content

Remove a value from an array without popping


freelance84

Recommended Posts

 

Just a quick one, new area for me.

 

I have an array which is looped against another array, if the two values from each ever match then an event occurs, if not then something else.

 

If a match does occur i then want to remove the said value from one of the arrays... is this really all i need to do, seems too simple:

 

unset($saved_dont_use[$j]);

Link to comment
Share on other sites

//get unique array values
$diff = array_diff($arr1, $arr2);

if (!empty($diff))
{
   //event trigger

   unset($arr1);
   //all unique values stored in $diff, $arr2 keeps all values.
}
else
{
   //event trigger
}

 

Will that do or do you need to trigger an event for each value matched?

 


foreach($arr1 as $k => $v)
{
   if ( in_array($arr1[$k], $arr2) )
   {
      //values matched, trigger event
      unset($arr1[$k]);
      // $arr1 contains unique values, $arr2 remains the same.
   }
   else
   {
      // no values matched, trigger other event.
   }
}

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.