PHP remove element from array by value

Remove element from php array by value in just 3 simple line code. PHP array functions are cool for doing this process. For removing element from array we need to provide key, but when we want to delete element by array value need some extra stuff.

By using array_flip function, after fliping key and value in array we able to delete array element by value.

Remove element from associative array:

//we want to delete element with value='sellername'
$nameArr = array('developer' => 'developername',
    'user' => 'username',
    'seller' => 'sellername');

//flip key and value
$nameArr = array_flip($nameArr);

//remove element
unset($nameArr['sellername']);

//flip again key and value
$nameArr = array_flip($nameArr);

print_r($nameArr);

Remove element from simple array:

//we want to delete element with value='developername'
$nameArr = array('developername','username','sellername');

//flip key and value
$index = array_search('developername', $nameArr);

//remove element
if($index !== false)
    unset($nameArr[$index]);

print_r($nameArr);