我有一个高尔夫应用程序。我在数据库中保持分数。我为特定用户提供了所有轮次,并返回了一个集合(数组:14)。每个项目包含190个项目(数组:190)。 190项是特定一轮高尔夫的得分数据。像每个洞的得分,每个洞的每个驱动器的距离等等。
我要做的是获得平均驾驶距离(所有带有驾驶距离数据的记录总计除以机会数。
诀窍是我想将这些分组分为5,5和4组。所以我想要最后4轮,前5轮以及前5轮的平均驾驶距离等。
我会将它们绘制在图表上。
我遇到的问题是在我对初始集合进行分块后访问(array:190)中的项目:
$rounds = Rounds::roundsAll($user, 14); //grabs me 14 rounds for a user.
$chunks = $rounds->chunk(5); // gives me 3 collections of 4-5-5
我知道我需要一个嵌套循环,如:
foreach ($chunks as $key => $chunk) {
foreach ($chunks as $key => $round) {
如果我没有分块,我可以访问收集项目,如:
$round->$holedrivedistance
或
$round->scorecard->$holepar // I have defined these relationship 'scorecard'
如何使用chunked集合执行此操作,以便我可以迭代每个组添加我需要添加的项目。然后为下一个块做同样的事情。
RoundsAll返回一个集合:
Collection {#277 ▼
#items: array:14 [▼
0 => Score {#281 ▼
#fillable: array:187 [▶]
#dates: array:1 [▶]
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:190 [▶]
#original: array:190 [▼
"id" => 15
"created_at" => "2017-03-13 15:31:04"
"updated_at" => "2017-03-13 15:31:04"
"user_id" => 1
"scorecard_id" => 42713
"course_id" => 10555
"club_id" => 9168
"total_score" => 79
"round_date" => "2017-03-09"
"round_type" => 18
...
"hole1_drive_distance" => 231
"hole2_drive_distance" => 261
"hole3_drive_distance" => 0
"hole4_drive_distance" => 351
"hole5_drive_distance" => 295
"hole6_drive_distance" => 305
"hole7_drive_distance" => 304
"hole8_drive_distance" => 198
"hole9_drive_distance" => 0
"hole10_drive_distance" => 267
"hole11_drive_distance" => 289
"hole12_drive_distance" => 0
"hole13_drive_distance" => 331
"hole14_drive_distance" => 261
"hole15_drive_distance" => 0
"hole16_drive_distance" => 234
"hole17_drive_distance" => 248
"hole18_drive_distance" => 0
..
]
#casts: []
#dateFormat: null
#appends: []
#events: []
#observables: []
#relations: array:1 [▶]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
1 => Score {#282 ▶}
2 => Score {#283 ▶}
3 => Score {#284 ▶}
4 => Score {#285 ▶}
5 => Score {#286 ▶}
6 => Score {#287 ▶}
7 => Score {#288 ▶}
8 => Score {#289 ▶}
9 => Score {#290 ▶}
10 => Score {#291 ▶}
11 => Score {#292 ▶}
12 => Score {#293 ▶}
13 => Score {#294 ▶}
]
}
Chunk返回按5-5-4
分组的相同集合我需要遍历“hole#_drive_distance”=>总计261项,然后用数字除以得到平均值。我成功地为所有轮次做了这个。问题是我不知道怎么做所以我回来了:
Chunk 1 = "AVG DRIVE"
Chunk 2 = "AVG DRIVE"
Chunk 3 = "AVG DRIVE"