in python have 2 list non-unique values:
a = [1,2,3,4,5,5,4,3,2,1,2,3,4,5] b = [1,2,2,2,5,5]
to substract b found solution:
from collections import counter mset subtract = mset(a) - mset(b) list(subtract.elements()) #result [1, 3, 3, 3, 4, 4, 4, 5]!!!!!!!!
how same in php? php not support lists.
array_diff not useful, because deletes non-unique values
a "functional" solution:
$a = [1,2,3,4,5,5,4,3,2,1,2,3,4,5]; $b = [1,2,2,2,5,5]; $bcopy = $b; $c = array_filter($a, function($item) use(&$bcopy) { $idx = array_search($item, $bcopy); // remove $b if found if($idx !== false) unset($bcopy[$idx]); // keep item if not found return $idx === false; }); sort($c); print_r($c);
you need make copy of $b
array_filter
callback destructive in regards array $b
. need sort result if want have exact same output in python.