Jump to content

[SOLVED] delete variable from array not by key


blurrydude

Recommended Posts

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.


//$_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.

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.