在laravel雄辩的集合上的Array_unique

时间:2022-10-25 09:38:59

Not sure if this is possible but im trying to run array_unique over a collection of items i have, to remove duplicates. Although i cannot get it working.

不确定这是否可行,但我试图在我拥有的项目集合上运行array_unique,以删除重复项。虽然我不能让它工作。

my controller logic:

我的控制器逻辑:

    // init models
    $jobs = Job::search();
    $countries = $jobs->get()->map(function( $job ) {

        return $job->country;
    });
    $countries = array_unique( $countries->toArray() );

although this gets a "Array to string conversion" error

虽然这会得到“数组到字符串转换”错误

4 个解决方案

#1


1  

You can have unique values in your DB results using distinct or group by in your select clause. But, if you really need to have unique values over an array of object you can do the following:

您可以在select子句中使用distinct或group by在数据库结果中包含唯一值。但是,如果您确实需要在对象数组上具有唯一值,则可以执行以下操作:

$uniques = array();
foreach ($countries as $c) {
    $uniques[$c->code] = $c; // Get unique country by code.
}

dd($uniques);

#2


27  

You could try the Unique method of the Collection class:

您可以尝试Collection类的Unique方法:

$countries = $countries->unique();

The Collection class has several wonderful methods. You could read about this in the Laravel API documentation.

Collection类有几个很棒的方法。您可以在Laravel API文档中阅读相关内容。

I agree that sometimes it is more efficient to "query" on an existing Collection in memory (in stead of doing another query on the database using the Querybuilder class), like for example you first want to count and then filter. In .NET LINQ you can query almost equally on an IEnumerable (in-memory collection) as on a database, something I really enjoy.

我同意有时在内存中对现有Collection进行“查询”更有效(而不是使用Querybuilder类对数据库进行另一次查询),例如你首先想要计算然后过滤。在.NET LINQ中,您可以在数据库上查询IEnumerable(内存中集合),这是我真正喜欢的。

#3


2  

I had similar issue and although time have passed since it may be useful to someone today.

我有类似的问题,虽然时间已经过去,因为它可能对今天的某些人有用。

The Problem I had was that when I called unique method on collection of items it didn't worked, that is probably the reason the first answer got accepted. So if you have models and you want to remove duplicates based on a specific field you can pass parameter to your unique method, in this case it would be:

我遇到的问题是当我在收集项目时调用了唯一的方法时,它没有起作用,这可能是第一个答案被接受的原因。因此,如果您有模型并且想要根据特定字段删除重复项,则可以将参数传递给您的唯一方法,在这种情况下,它将是:

$countries->unique('code');

that way you'll only have countries with unique codes. You may notice that only the first value stays, so if you develop a shopping cart application and want for some reason merge carts and only want to have recent items you can just reverse the collection and call unique and reverse it back:

这样你只会拥有唯一代码的国家/地区。您可能会注意到只有第一个值保留,因此如果您开发购物车应用程序并且由于某种原因想要合并购物车并且只想拥有最近的项目,您可以反转该集合并调用unique并将其反转:

$countries->reverse()->unique('code')->reverse(); // it doesn't really make sense in this example though

it is probably not the best option and it is better to do filtering on the database side but it is nice to have options.

它可能不是最好的选择,最好在数据库端进行过滤,但有选择是很好的。

#4


0  

You could try the filter method on eloquent collections if that's exactly what you want to do

你可以在雄辩的集合上尝试过滤方法,如果这正是你想要做的

http://laravel.com/docs/eloquent#collections

http://laravel.com/docs/eloquent#collections

#1


1  

You can have unique values in your DB results using distinct or group by in your select clause. But, if you really need to have unique values over an array of object you can do the following:

您可以在select子句中使用distinct或group by在数据库结果中包含唯一值。但是,如果您确实需要在对象数组上具有唯一值,则可以执行以下操作:

$uniques = array();
foreach ($countries as $c) {
    $uniques[$c->code] = $c; // Get unique country by code.
}

dd($uniques);

#2


27  

You could try the Unique method of the Collection class:

您可以尝试Collection类的Unique方法:

$countries = $countries->unique();

The Collection class has several wonderful methods. You could read about this in the Laravel API documentation.

Collection类有几个很棒的方法。您可以在Laravel API文档中阅读相关内容。

I agree that sometimes it is more efficient to "query" on an existing Collection in memory (in stead of doing another query on the database using the Querybuilder class), like for example you first want to count and then filter. In .NET LINQ you can query almost equally on an IEnumerable (in-memory collection) as on a database, something I really enjoy.

我同意有时在内存中对现有Collection进行“查询”更有效(而不是使用Querybuilder类对数据库进行另一次查询),例如你首先想要计算然后过滤。在.NET LINQ中,您可以在数据库上查询IEnumerable(内存中集合),这是我真正喜欢的。

#3


2  

I had similar issue and although time have passed since it may be useful to someone today.

我有类似的问题,虽然时间已经过去,因为它可能对今天的某些人有用。

The Problem I had was that when I called unique method on collection of items it didn't worked, that is probably the reason the first answer got accepted. So if you have models and you want to remove duplicates based on a specific field you can pass parameter to your unique method, in this case it would be:

我遇到的问题是当我在收集项目时调用了唯一的方法时,它没有起作用,这可能是第一个答案被接受的原因。因此,如果您有模型并且想要根据特定字段删除重复项,则可以将参数传递给您的唯一方法,在这种情况下,它将是:

$countries->unique('code');

that way you'll only have countries with unique codes. You may notice that only the first value stays, so if you develop a shopping cart application and want for some reason merge carts and only want to have recent items you can just reverse the collection and call unique and reverse it back:

这样你只会拥有唯一代码的国家/地区。您可能会注意到只有第一个值保留,因此如果您开发购物车应用程序并且由于某种原因想要合并购物车并且只想拥有最近的项目,您可以反转该集合并调用unique并将其反转:

$countries->reverse()->unique('code')->reverse(); // it doesn't really make sense in this example though

it is probably not the best option and it is better to do filtering on the database side but it is nice to have options.

它可能不是最好的选择,最好在数据库端进行过滤,但有选择是很好的。

#4


0  

You could try the filter method on eloquent collections if that's exactly what you want to do

你可以在雄辩的集合上尝试过滤方法,如果这正是你想要做的

http://laravel.com/docs/eloquent#collections

http://laravel.com/docs/eloquent#collections