blurrydude Posted April 14, 2008 Share Posted April 14, 2008 I tried several variations of unset and some different array_diff combinations, but i cant seem to do this: the script needs to take an array, examine it to find a specific value that the user will input and then remove it from the array. i.e.: array - red yellow orange user inputs yellow array - red orange seems simple, but I can't seem to figure out how without first knowing the key. Link to comment https://forums.phpfreaks.com/topic/101004-solved-delete-variable-from-array-not-by-key/ Share on other sites More sharing options...
papaface Posted April 14, 2008 Share Posted April 14, 2008 //$_POST['colour'] = "yellow"; $array = array(0 => 'yellow', 1 => 'red', 2 => 'orange'); $key = array_search($_POST['colour'], $array); // $key = 1; unset($array[$key]); ?> Should work, untested though. It assumes $_POST['colour'] contains the name of the element to be removed. Link to comment https://forums.phpfreaks.com/topic/101004-solved-delete-variable-from-array-not-by-key/#findComment-516518 Share on other sites More sharing options...
papaface Posted April 14, 2008 Share Posted April 14, 2008 You could simplify it by doing: <?php //$_POST['colour'] = "yellow"; $array = array(0 => 'yellow', 1 => 'red', 2 => 'orange'); unset($array[array_search($_POST['colour'], $array)]); // $key = 1; ?> Link to comment https://forums.phpfreaks.com/topic/101004-solved-delete-variable-from-array-not-by-key/#findComment-516526 Share on other sites More sharing options...
blurrydude Posted April 14, 2008 Author Share Posted April 14, 2008 Thanks, works fine. I knew I was missing something simple. Link to comment https://forums.phpfreaks.com/topic/101004-solved-delete-variable-from-array-not-by-key/#findComment-516920 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.