Is it possible to save the fields of one array as a new array?

In my CakePHP application, I drag out the results of a table called Schedules. This table has an athlete_id and several splits for the race. I need to make some calculations at one time of an athlete compared to another athlete (for example, to calculate the difference between the fastest ending time and the slowest).

I thought the best way to do this is to save all the athletes tokens in a new array, and then run the min () function on it. For the life of me I cannot make it work ...

Do you think I'm approaching this wrong, or am I just missing something?

foreach ($timesheet['Run'] as $run):
    <tr<?php echo $class;?>>
        <td><?php echo $run['athlete_id'];?></td>
        <td><?php echo $run['start'];?></td>
        <td><?php echo $run['split1'];?></td>
        <td><?php echo $run['split2'];?></td>
        <td><?php echo $run['split3'];?></td>
        <td><?php echo $run['split4'];?></td>
        <td><?php echo $run['split5'];?></td>
        <td><?php echo $run['split6'];?></td>
        <td><?php echo $run['finish'];?></td>
    </tr>
   endforeach
+3
source share
5 answers

, , asc , - ( ):

$fastest = $previous = $timesheet['Run'][0]['finish'];
foreach ($timesheet['Run'] as $run):
    <tr>
        <td><?php echo $run['athlete_id'];?></td>
        <td><?php echo $run['finish'];?></td>
        <td><?php echo $previous - $run['finish'];?></td>
        <td><?php echo $fastest - $run['finish'];?></td>
    </tr>
    $previous = $run['finish'];
endforeach

:

id | finished_in | to_previous  | to_best
1  | 120         | 0            | 0
2  | 121.5       | -1.5         | -1.5
3  | 121.8       | -0.3         | -1.8
4  | 122.6       | -0.8         | -2.6
...

- , , ArrayIterator, , $fastest $previous , .

+1

, foreach ';' .

, , $timesheet ['Run'] [0] ['athlete_id']? print_r(), , $timesheet , .

0

- usort. $timesheet ['Run'] "finish". , , , .

:

function cmp($a, $b)
{
   if ($a['finish'] == $b['finish'])
   { 
      return 0;
   }

   return ($a['finish'] < $b['finish']) ? -1 : 1;
}

usort($timesheet['Run'], "cmp");
0
source

I recommend either performing calculations and sorting in the controller or in SQL.

0
source

In my opinion, you need to change the way you store your data. try creating a table containing id, event_id, athlete_id and time, which will make your application more flexible and simplify calculations.

0
source

Source: https://habr.com/ru/post/1722529/


All Articles