English 中文(简体)
Is it possible to store the fields of one Array as a new array?
原标题:
  • 时间:2009-11-11 01:05:08
  •  标签:
  • php
  • cakephp

In my CakePHP app, I m pulling in the results of a table called Timesheets . This table has an athlete_id and a few split times for a race. I need to do some calculations on one athlete s times compared to another athlete (for instance, calculate the difference between the fastest finish time and slowest).

I thought the best way to do this would be to store every athletes finish in a new array, then run the min() function on it. For the life of me, I can t get this to work...

Do you think I am approaching this the wrong way, 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
最佳回答

Assuming you are fetching the timetable with order by finish asc from the database, you could do something like this (omitting splits for brevity):

$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

This should give rows like this:

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
...

Another option would be to wrap the returned array into an Object or maybe an ArrayIterator and do the calulcations inside, encapsuling $fastest and $previous and whatever else you might want to calculate.

问题回答

I ll assume you have the appropriate tags around the foreach and a ; after endforeach.

So the question becomes can you access $timesheet[ Run ][0][ athlete_id ]? You might use print_r() to make sure that $timesheet is in the form you think it is.

I would look into using something like usort. You can define a callback function to sort your $timesheet[ Run ] array by the finish key. In the end, the first entry in your array, if defined correctly, will have the shortest finish time.

For example:

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

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

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

I recommend either doing the calculation and sorting in the controller and in SQL.

In my opinion you need to change the way you store your data. try to create table containing id, event_id, athlete_id and time, that would make your application more flexible and calculation easier.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签