I have associative array like below
$arr = [1=>0, 2=>1, 3=>1, 4=>2]
I would like to remove the duplicate values from the initial array and return those duplicates as a new array. So I would end up with something like;
$arr = [1=>0, 4=>2]
$new_arr = [2=>1, 3=>1]
Does PHP provide such a function or if not how would I achieve this?
Try:
Use
array_filter()
to get all duplicate values from arrayUse
array_diff()
to get all unique values from arrayOutput:
You can get unique values from an array using array_unique and then compare the resulting array with array_diff_assoc
This will keep the indexes for both arrays, here's an example:
And the result: