Reputation: 7693
This is the multidimensional array and there's two column one is string and other one is laravel collection model.
array:2 [▼
0 => array:2 [▼
"email" => "[email protected]"
"user" => User {#2508 ▶}
]
1 => array:2 [▼
"email" => "[email protected]"
"user" => User {#2547 ▶} //laravel collection
]
]
I used php multi-dimensional array remove duplicate and it won't work for this.
I wan't to remove duplicate columns by email (no need to looked at the laravel user model.) Are there any inbuilt PHP function for this ?
Upvotes: 0
Views: 266
Reputation: 16283
You can use Laravel's Collection::unique
method:
$unique = collect($yourArray)->unique('email');
By passing in email
into the method, you're telling Laravel to look at that specific field in your dataset, rather than the data as a whole. You can then convert it back to an array using toArray
.
$unique = collect($yourArray)->unique('email')->toArray();
You can also pass a closure to the unique
method to allow you to define the value that you want to compare for each object to determine uniqueness:
$unique = collect($yourArray)->unique(function ($item) {
return $item['email'];
});
Upvotes: 1