Laravel 分组 collection 并按最大值排序
Laravel group collection and sort by the biggest value
我有一个 Laravel collection 的值,我想按 'type' 分组并按最大的 'probability' 排序。这是我的 collection 的样子:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => Array
(
[type] => correct-score
[name] => 1-1
[probability] => 10.599257670442
[probability_odd] => 10.142219704667
[odd] =>
[value_bet] => 0
[prognose_type_id] => 12
[value_bet_diff] => -10.142219704667
)
[1] => Array
(
[type] => 3-way
[name] => 1
[probability] => 52.459602877727
[probability_odd] => 2.0491958402842
[prognose_type_id] => 1
[odd] => 2.48
[value_bet_diff] => 0.4308041597158
)
[2] => Array
(
[type] => 3-way
[name] => 2
[probability] => 24.682032371379
[probability_odd] => 4.3553949845985
[prognose_type_id] => 1
[odd] => 2.66
[value_bet_diff] => -1.6953949845985
)
[3] => Array
(
[type] => 3-way
[name] => X
[probability] => 22.857984044926
[probability_odd] => 4.7029519221256
[prognose_type_id] => 1
[odd] => 3.7
[value_bet_diff] => -1.0029519221256
)
[4] => Array
(
[type] => double-chance
[name] => 1X
[probability] => 37.658793461327
[probability_odd] => 2.8545789739758
[prognose_type_id] => 4
[odd] => 1.49
[value_bet_diff] => -1.3645789739758
)
[5] => Array
(
[type] => double-chance
[name] => X2
[probability] => 23.770008208152
[probability_odd] => 4.5225058005294
[prognose_type_id] => 4
[odd] => 1.55
[value_bet_diff] => -2.9725058005294
)
[6] => Array
(
[type] => double-chance
[name] => 12
[probability] => 38.570817624553
[probability_odd] => 2.7870811826288
[prognose_type_id] => 4
[odd] => 1.29
[value_bet_diff] => -1.4970811826288
)
[7] => Array
(
[type] => both-to-score
[name] => yes
[probability] => 58.505776168275
[probability_odd] => 1.8374254140447
[prognose_type_id] => 8
[odd] =>
[value_bet_diff] => -1.8374254140447
)
我想达到这样的结果:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => Array
(
[type] => correct-score
[name] => 1-1
[probability] => 10.599257670442
[probability_odd] => 10.142219704667
[odd] =>
[value_bet] => 0
[prognose_type_id] => 12
[value_bet_diff] => -10.142219704667
)
[1] => Array
(
[type] => 3-way
[name] => 1
[probability] => 52.459602877727
[probability_odd] => 2.0491958402842
[prognose_type_id] => 1
[odd] => 2.48
[value_bet_diff] => 0.4308041597158
)
[6] => Array
(
[type] => double-chance
[name] => 12
[probability] => 38.570817624553
[probability_odd] => 2.7870811826288
[prognose_type_id] => 4
[odd] => 1.29
[value_bet_diff] => -1.4970811826288
)
[7] => Array
(
[type] => both-to-score
[name] => yes
[probability] => 58.505776168275
[probability_odd] => 1.8374254140447
[prognose_type_id] => 8
[odd] =>
[value_bet_diff] => -1.8374254140447
)
一直没能找到类似的解决方案,所以我最终决定在这里试试。你能帮帮我吗?
提前致谢!
我认为下面可以工作;
$collection->sortByDesc('probability')->groupBy('type');
按概率排序而不是按类型分组;
在 Eloquent 中执行此操作可能更好,但由于您已将其作为 collection 的问题提出;
假设您的 collection 被称为 $items
$sorted = $items->groupBy('type')
->map(function($group){
return $group->sortByDesc('probability')->take(1);
})
->flatten(1);
最短:
$items = $collections
->sortByDesc('probability')
->unique('type');
我有一个 Laravel collection 的值,我想按 'type' 分组并按最大的 'probability' 排序。这是我的 collection 的样子:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => Array
(
[type] => correct-score
[name] => 1-1
[probability] => 10.599257670442
[probability_odd] => 10.142219704667
[odd] =>
[value_bet] => 0
[prognose_type_id] => 12
[value_bet_diff] => -10.142219704667
)
[1] => Array
(
[type] => 3-way
[name] => 1
[probability] => 52.459602877727
[probability_odd] => 2.0491958402842
[prognose_type_id] => 1
[odd] => 2.48
[value_bet_diff] => 0.4308041597158
)
[2] => Array
(
[type] => 3-way
[name] => 2
[probability] => 24.682032371379
[probability_odd] => 4.3553949845985
[prognose_type_id] => 1
[odd] => 2.66
[value_bet_diff] => -1.6953949845985
)
[3] => Array
(
[type] => 3-way
[name] => X
[probability] => 22.857984044926
[probability_odd] => 4.7029519221256
[prognose_type_id] => 1
[odd] => 3.7
[value_bet_diff] => -1.0029519221256
)
[4] => Array
(
[type] => double-chance
[name] => 1X
[probability] => 37.658793461327
[probability_odd] => 2.8545789739758
[prognose_type_id] => 4
[odd] => 1.49
[value_bet_diff] => -1.3645789739758
)
[5] => Array
(
[type] => double-chance
[name] => X2
[probability] => 23.770008208152
[probability_odd] => 4.5225058005294
[prognose_type_id] => 4
[odd] => 1.55
[value_bet_diff] => -2.9725058005294
)
[6] => Array
(
[type] => double-chance
[name] => 12
[probability] => 38.570817624553
[probability_odd] => 2.7870811826288
[prognose_type_id] => 4
[odd] => 1.29
[value_bet_diff] => -1.4970811826288
)
[7] => Array
(
[type] => both-to-score
[name] => yes
[probability] => 58.505776168275
[probability_odd] => 1.8374254140447
[prognose_type_id] => 8
[odd] =>
[value_bet_diff] => -1.8374254140447
)
我想达到这样的结果:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => Array
(
[type] => correct-score
[name] => 1-1
[probability] => 10.599257670442
[probability_odd] => 10.142219704667
[odd] =>
[value_bet] => 0
[prognose_type_id] => 12
[value_bet_diff] => -10.142219704667
)
[1] => Array
(
[type] => 3-way
[name] => 1
[probability] => 52.459602877727
[probability_odd] => 2.0491958402842
[prognose_type_id] => 1
[odd] => 2.48
[value_bet_diff] => 0.4308041597158
)
[6] => Array
(
[type] => double-chance
[name] => 12
[probability] => 38.570817624553
[probability_odd] => 2.7870811826288
[prognose_type_id] => 4
[odd] => 1.29
[value_bet_diff] => -1.4970811826288
)
[7] => Array
(
[type] => both-to-score
[name] => yes
[probability] => 58.505776168275
[probability_odd] => 1.8374254140447
[prognose_type_id] => 8
[odd] =>
[value_bet_diff] => -1.8374254140447
)
一直没能找到类似的解决方案,所以我最终决定在这里试试。你能帮帮我吗?
提前致谢!
我认为下面可以工作;
$collection->sortByDesc('probability')->groupBy('type');
按概率排序而不是按类型分组;
在 Eloquent 中执行此操作可能更好,但由于您已将其作为 collection 的问题提出;
假设您的 collection 被称为 $items
$sorted = $items->groupBy('type')
->map(function($group){
return $group->sortByDesc('probability')->take(1);
})
->flatten(1);
最短:
$items = $collections
->sortByDesc('probability')
->unique('type');