Skip to content

Instantly share code, notes, and snippets.

@midnite81
Created October 30, 2017 12:43
Show Gist options
  • Save midnite81/9f6024e235df726e6940e0a1ed98c127 to your computer and use it in GitHub Desktop.
Save midnite81/9f6024e235df726e6940e0a1ed98c127 to your computer and use it in GitHub Desktop.
Remove a value from an array
<?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