0

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" => "myfirstemail@gmail.com"
    "user" => User {#2508 ▶}
  ]
  1 => array:2 [▼
    "email" => "myfirstemail@gmail.com"
    "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 ?

Elshan
  • 7,339
  • 4
  • 71
  • 106

1 Answers1

1

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'];
});
Jonathon
  • 15,873
  • 11
  • 73
  • 92