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]);

//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.
   }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.