18

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?

1
  • Although terribly common back in 2009, this question does not make a good dupe target because it is vague about its requirements and is missing its minimal reproducible example. Commented Feb 7, 2023 at 22:19

6 Answers 6

33

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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.

Comments

2

The array_diff function will do this for you.

Comments

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

print_r($result);

Comments

1

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.

Comments

1

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]);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.