freelance84 Posted September 17, 2010 Share Posted September 17, 2010 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 https://forums.phpfreaks.com/topic/213675-remove-a-value-from-an-array-without-popping/ Share on other sites More sharing options...
Andy-H Posted September 17, 2010 Share Posted September 17, 2010 //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 https://forums.phpfreaks.com/topic/213675-remove-a-value-from-an-array-without-popping/#findComment-1112171 Share on other sites More sharing options...
freelance84 Posted September 20, 2010 Author Share Posted September 20, 2010 Yea thanks, I think the second method you posted will work. Is it not really a good idea to remove a value from an array with just the unset function? Link to comment https://forums.phpfreaks.com/topic/213675-remove-a-value-from-an-array-without-popping/#findComment-1113440 Share on other sites More sharing options...
freelance84 Posted September 21, 2010 Author Share Posted September 21, 2010 Thanks Andy-H, using "in_array" works like a charm. That's one more function learnt Link to comment https://forums.phpfreaks.com/topic/213675-remove-a-value-from-an-array-without-popping/#findComment-1113703 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.