Laravel remove objects with duplicate values in array

Anil Meena

My application returns an array of objects and each object contains a "name" key which sometimes is repeated in other objects.

How can I filter the duplicates?

This is an array having duplicates

[
    {"name":"India","id":2,"uid":"1M16"},
    {"name":"Delhi","id":3,"uid":"1M16"},
    {"name":"India","id":4,"uid":"1M16"}
]

The output I want is:

[
    {"name":"India","id":2,"uid":"1M16"},
    {"name":"Delhi","id":3,"uid":"1M16"}
]

And this is how I am getting this array from database.

$results_1 = Sheet::whereIn('uid', $results_1)->select('name', 'id', 'uid')->get(); 
zahid hasan emon

You can use distinct() if you want a single column. In your case you need groupBy() to get the unique values.

$results_2 = Sheet::whereIn('uid', $results_1)->select('name', 'id', 'uid')->groupBy('name')->get();

you are using same $results_1 variable to hold the new values and in whereIn clause. consider using different variables.

And to use groupBy() you have to change some things in your database config file. in your config>database.php file add this lines in mysql configuration.

'modes' => [

    'STRICT_TRANS_TABLES',
    'NO_ZERO_IN_DATE',
    'NO_ZERO_DATE',
    'ERROR_FOR_DIVISION_BY_ZERO',
    'NO_AUTO_CREATE_USER',
    'NO_ENGINE_SUBSTITUTION'
]

And don't forget to clear the cache after changing anything in the config file.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Remove Duplicate objects from JSON Array

Remove entrySet objects with duplicate field values

js remove duplicate from array of objects by values and count duplicates

Remove objects with duplicate properties from Swift array

Remove duplicate values from an array of objects in javascript

Remove duplicate values in a tuple array

How to remove duplicate values in array?

How to remove both duplicate values in an array of objects

How to delete duplicate values of objects inside array?

Javascript - Remove duplicate ID from array of objects

Remove Duplicate Array and Merge The Values

Compare Objects in Array and Remove Duplicate & Update - Javascript

Remove duplicate array objects mongodb

Remove duplicate objects in JSON and form an array

Remove objects with duplicate date property from Array

PHP remove duplicate array values

How to remove duplicate data values from an array of objects in javascript?

Remove first duplicate from an array of objects in JavaScript

Javascript: Remove duplicate objects from array with conditions

Grouping duplicate objects into an array of values

Combining objects with duplicate values inside an array

Remove all instances of duplicate objects in a javascript array

Merge properties of objects within a array together using values and remove duplicate

Remove duplicate values and overwrite the array

Remove duplicate objects from an array with Keys

how to remove first element of duplicate in an array of objects

JQ - Remove Duplicate Array Values

remove duplicate from array of objects based on condition

Remove objects with duplicate id and sorted values