查找具有匹配值的数组元素

时间:2017-02-22 11:20:49

标签: php arrays codeigniter multidimensional-array

我有一个包含多个对象的数组,其中一些对象可能具有lap_time的匹配值,在此示例中为元素12具有匹配的值:

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;
    }
} 

你能否提出任何可能对我有帮助的功能,或者代码示例

2 个答案:

答案 0 :(得分:0)

试试这个..

class Results_calculations_model
{
    public
    function race_points($results)
    {
        $results_with_points = $this->break_race_ties($results);

        return $results_with_points;
    }

    protected
    function break_race_ties($results)
    {
    $previous = null;
    foreach($results as &$current) {
        if(!empty($previous)) {
        $this->average_array_values($current, $previous);
        }
        $previous = $current;
    }
    }

    protected
    function average_array_values(&$current, &$previous)
    {
        if($current->lap_time == $previous->lap_time) {
            $avg = ($current->points + $previous->points) / 2;
            $current->points = $previous->points = $avg;
        }
    }
} 

答案 1 :(得分:0)

这就是我最终做的事情

<div>
    @Html.Action("/Views/Partial/_PartView.cshtml")
</div>