Removing duplicates from an object
I am using laravel5.4, I am getting the object as [1, 1, 1, 1]
I want the duplicates to be removed and display the result as [1]
How can i do this? I tried to hide it into an array and used the array_unique function. But it doesn't work.
+1
Kayal
source
to share
2 answers
If this is a collection you can try this
$collection = collect([1, 1, 2, 2, 3, 4, 2]);
$unique = $collection->unique();
$unique->values()->all();
//result [1, 2, 3, 4]
+1
Nikhil Radadiya
source
to share
Assuming [1,1,1,1] is collection
, you can use laravel method unique
on collection values: https://laravel.com/docs/master/collections#method-unique
I am a simple array, I think you can convert it to a collection before.
+1
Amarnasan
source
to share