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" => "[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 ?

1
  • @ThisGuyHasTwoThumbs not work for me.. Commented Jun 12, 2018 at 7:56

1 Answer 1

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'];
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.