If you find yourself with an array that you know contains a certain value that you need to access or erase here is a useful code snippet that I came across the other day.
For example, given the following array:
- $colors = array( 'red', 'blue', 'green', 'yellow' );
If you wanted to get rid of 'green' but are unsure of its key:
Or you could change it to 'brown' with:
- $key = array_search( 'green', $colors );
- $colors[$key] = 'brown';