Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing all instances of items from array

Tags:

arrays

php

I have an array which may have duplicate values

$array1 = [value19, value16, value17, value16, value16]

I'm looking for an efficient little PHP function that could accept either an array or a string (whichever makes it easier)

$array2 = ["value1", "value16", "value17"];
or 
$string2 = "value1 value16 value17";

and removes each item in array2 or string2 from array1.

The right output for this example would be:

$array1 = [value19]

For those more experienced with PHP, is something like this available in PHP?

like image 463
Lingo Avatar asked Aug 28 '10 06:08

Lingo


1 Answers

you're looking for array_diff

$array1 = array('19','16','17','16','16');
$array2 = array('1','16','17');
print_r(array_diff($array1,$array2));

Array ( [0] => 19 )

like image 195
Galen Avatar answered Sep 25 '22 04:09

Galen