solvit.io

Remove a specific element from a PHP array

Solution
function removeElement($array, $element)
{
    return array_diff($array, array($element));
}
Example
$array = [4, 8, 'foo', 15, 16, 'bar', 23, 42, 'bar'];
$array = removeElement($array, 'bar');
// $array = [4, 8, 'foo', 15, 16, 23, 42];

$array = removeElement($array, 23);
// $array = [4, 8, 'foo', 15, 16, 42];
Find more solutions about: ,