Created
October 30, 2017 12:43
-
-
Save midnite81/9f6024e235df726e6940e0a1ed98c127 to your computer and use it in GitHub Desktop.
Remove a value from an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Remove a value from an array | |
* | |
* @param $valueToRemove | |
* @param $array | |
* @return bool | |
*/ | |
function array_remove_value($valueToRemove, $array) { | |
if (($key = array_search($valueToRemove, $array)) !== false) { | |
unset($array[$key]); | |
} | |
return $array; | |
} | |
// Example usage: | |
$array = [ | |
'dave', | |
'peter', | |
'sandra' | |
]; | |
$array = array_remove_value('peter', $array); | |
/** | |
* Output | |
* | |
* [ | |
* 'dave', | |
* 'sandra' | |
* ] | |
* / |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment