How to remove values from an array in PHP?

Is there a PHP function to remove certain array elements from an array?

E.g., I have an array (A) with values and another array (B) from which a need to remove values.

Want to remove the values in array A from array B?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Use array_diff()

$new_array = array_diff($arrayB, $arrayA);

will return an array with all the elements from $arrayB that are not in $arrayA.

To do this with associative arrays use array_diff_assoc().

To remove a single value use:

unset($array[$key]);

You can of course loop that to do the equivalent of the array functions but there’s no point in that.

Solution 2

It depends on what you mean by “remove”.

You can use the unset() function to remove keys from your array, but this will not reindex it. So for example, if you have:

$a = array(1 => 'one', 2 => 'two', 3 => 'three');

and you then call

unset($a[2]);

You’ll end up with something like

(1 => 'one', 3 => 'three');

If you need the array to be sequentially indexed, you can take the unsetted array and feed it into array_values(), which will return a new array with sequentially indexed keys.

Returning to your original scenario, as others observe, array_diff will do the job for you, but note that it doesn’t do an index check. If you need that, use array_diff_assoc, instead.

Solution 3

The array_diff function will do this for you.

Solution 4

$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);

Solution 5

I came accros this post looking for a way to remove one single value from an array (which the title states). Here is a straight forward way, assuming the value to remove is in the array:

$list = array('foo', 'bar', 'yay', '\o/');
$toremove = 'foo';
unset($list[array_search($toremove, $list)]);

Though this will throw errors if the element to remove is not part of the array.

Another solution, but not really optimised performance wise is:

$list = array('foo', 'bar', 'yay', '\o/');
$toremove = 'foo';
$list = array_flip($list);
unset($list[$toremove]);
$list = array_flip($list);

Anyway, perhaps creating an array with the single value as using array_diff as suggested by everyone here is quicker and more efficient.

Solution 6

Perhaps a simple and quicker solution is this. A simple reiteration that can delete(unset) multiple values in the array.

    // Your array
    $list = array("apple", "orange", "strawberry", "lemon", "banana");

    // Initilize what to delete
    $delete_val = array("orange", "lemon", "banana");

    // Search for the array key and unset   
    foreach($delete_val as $key){
        $keyToDelete = array_search($key, $list);
        unset($list[$keyToDelete]);
    }

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply