-1
我有一個包含多個對象的陣列,其中一些對象可以具有用於lap_time
匹配值,在這個例子元件1
,和2
具有匹配的值:查找陣列元件具有匹配值
Array
(
[0] => stdClass Object
(
[id] => 1
[recorded_time] => 00:51:55.000000
[corrected_time] => 00:45:32.456140
[lap_time] => 00:09:06.491228
[laps] => 5
[code] =>
[sail_no] => 4931
[class] => SOLO
[helm_fname] => [Removed]
[helm_lname] => [Removed]
[crew_1_fname] =>
[crew_1_lname] =>
[crew_2_fname] =>
[crew_2_lname] =>
[modified_time] =>
[created_time] => 2017-02-19 17:53:48
[created_fname] => Admin
[created_lname] => istrator
[modified_fname] =>
[modified_lname] =>
[points] => 1
)
[1] => stdClass Object
(
[id] => 21
[recorded_time] => 00:50:07.000000
[corrected_time] => 00:45:56.186984
[lap_time] => 00:09:11.237397
[laps] => 5
[code] =>
[sail_no] => 67173
[class] => LASER
[helm_fname] => [Removed]
[helm_lname] => [Removed]
[crew_1_fname] =>
[crew_1_lname] =>
[crew_2_fname] =>
[crew_2_lname] =>
[modified_time] => 2017-02-22 10:51:58
[created_time] => 2017-02-19 18:40:58
[created_fname] => Admin
[created_lname] => istrator
[modified_fname] =>
[modified_lname] =>
[points] => 2
)
[2] => stdClass Object
(
[id] => 2
[recorded_time] => 00:50:07.000000
[corrected_time] => 00:45:56.186984
[lap_time] => 00:09:11.237397
[laps] => 5
[code] =>
[sail_no] => 52441
[class] => LASER
[helm_fname] => [Removed]
[helm_lname] => [Removed]
[crew_1_fname] =>
[crew_1_lname] =>
[crew_2_fname] =>
[crew_2_lname] =>
[modified_time] => 2017-02-22 10:51:58
[created_time] => 2017-02-19 18:40:58
[created_fname] => Admin
[created_lname] => istrator
[modified_fname] =>
[modified_lname] =>
[points] => 3
)
如果任何對象的匹配值爲lap_time
,匹配對象需要對它們的點進行平均。
這就是我開始做的,但我意識到這可能不是最好的方式去做這件事。 break_race_ties
方法尚未完成。
<?php
class Results_calculations_model
{
public
function race_points($results)
{
$points = 0;
$results_with_points = array();
foreach ($results AS $result) {
//increment points by one for each position
$points++;
$result->points = $points;
//add result back to array;
$results_with_points[] = $result;
}
$results_with_points = $this->break_race_ties($results_with_points);
return $results_with_points;
}
protected
function break_race_ties($results)
{
$previous_time = 0;
$ties = array();
foreach ($results AS $result) {
if ($previous_time === $result->lap_time) {
$ties['points'][] = $result->points;
$ties['id'][] = $result->id;
$average_points = $this->average_array_values($ties['points']);
}
$previous_time = $result->lap_time;
}
}
protected
function average_array_values($values)
{
$number_of_elements = count($values);
$total_points = array_sum($values);
return $total_points/$number_of_elements;
}
}
你可以建議可能會幫助我的任何功能,或做什麼
如果3個或更多關係出現在單個'lap_time'中,這會起作用嗎? –