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. Quote 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. Quote 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; ?> Quote 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. Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.