Php remove item in array by value

Borrowed the logic of underscore.JS _.reject and created two functions (people prefer functions!!)

array_reject_value: This function is simply rejecting the value specified (also works for PHP4,5,7)

function array_reject_value(array &$arrayToFilter, $deleteValue) {
    $filteredArray = array();

    foreach ($arrayToFilter as $key => $value) {
        if ($value !== $deleteValue) {
            $filteredArray[] = $value;
        }
    }

    return $filteredArray;
}

array_reject: This function is simply rejecting the callable method (works for PHP >=5.3)

function array_reject(array &$arrayToFilter, callable $rejectCallback) {

    $filteredArray = array();

    foreach ($arrayToFilter as $key => $value) {
        if (!$rejectCallback($value, $key)) {
            $filteredArray[] = $value;
        }
    }

    return $filteredArray;
}

So in our current example we can use the above functions as follows:

$messages = [312, 401, 1599, 3, 6];
$messages = array_reject_value($messages, 401);

or even better: (as this give us a better syntax to use like the array_filter one)

$messages = [312, 401, 1599, 3, 6];
$messages = array_reject($messages, function ($value) {
    return $value === 401;
});

The above can be used for more complicated stuff like let's say we would like to remove all the values that are greater or equal to 401 we could simply do this:

$messages = [312, 401, 1599, 3, 6];
$greaterOrEqualThan = 401;
$messages = array_reject($messages, function ($value) use $greaterOrEqualThan {
    return $value >= $greaterOrEqualThan;
});

To remove the element from an array by value, we can use the combination of array_search() and unset() functions in PHP.

Here is an example, that removes the second element "green" from the colors array by value.

<?php
$colors = array("red", "green", "yellow", "orange");

if (($key = array_search("green", $colors)) !== false) {
    unset($colors[$key]);
}

print_r($colors)
?>

Output:

Array
(
    [0] => red
    [2] => yellow
    [3] => orange
)

The unset() function doesn’t re-index the array keys, if you want to re-index it then you can use the array_values() function after unset().

<?php
$colors = array("red", "green", "yellow", "orange");

if (($key = array_search("green", $colors)) !== false) {
    unset($colors[$key]);
}

print_r(array_values($colors))
?>

Similarly, we can also use the array_diff() function like this to remove it.

$colors = array("red", "green", "yellow", "orange");

$newArray = array_diff($colors, array("green"));

print_r($newArray)

You can also use the array_diff() function to remove multiple elements by using value.

$colors = array("red", "green", "yellow", "orange");

$newArray = array_diff($colors, array("green", "yellow"));

print_r($newArray)

Note: The array_diff() function creates a new array by preserving the original array, where the first solution modifies the original array.

How do I remove a specific element from an array in PHP?

In order to remove an element from an array, we can use unset() function which removes the element from an array and then use array_values() function which indexes the array numerically automatically. Function Used: unset(): This function unsets a given variable.

How do I remove a specific element from an array?

pop() function: This method is use to remove elements from the end of an array. shift() function: This method is use to remove elements from the start of an array. splice() function: This method is use to remove elements from the specific index of an array.

Which function is used to remove or delete elements from an array in PHP?

The array_splice() function removes selected elements from an array and replaces it with new elements. The function also returns an array with the removed elements.

Does PHP have flip function?

The array_flip() function is used to exchange the keys with their associated values in an array. The function returns an array in flip order, i.e. keys from array become values and values from array become keys. Note: The values of the array need to be valid keys, i.e. they need to be either integer or string.