Mutley Posted August 9, 2007 Share Posted August 9, 2007 I have some numbers: $a = '26'; $b = '25.5'; $c = '24'; $d = '14'; ...etc What I wish to do, is for every .5 = 1 second from the highest number. So, since $a is the highest number, I want it to echo something like this: 0s +1s (this is $b) +4s (this is $c) +24s (this is $d) if that makes sense? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/64080-assigning-a-time-to-a-number-range/ Share on other sites More sharing options...
HuggieBear Posted August 9, 2007 Share Posted August 9, 2007 There's probably a neater way of doing this, but I'd go with this: <?php // Array of timings to pass in to function $times[] = "26"; $times[] = "25.5"; $times[] = "24"; $times[] = "14"; // Call to function $secs = SecondDelay($times); // Print out each timing difference in 'seconds' foreach ($secs as $t){ echo $t . "<br>"; } // Funtion takes an unordered array of times and returns an ordered array of differences function SecondDelay($timings){ rsort($timings, SORT_NUMERIC); $highest = $timings[0]; foreach ($timings as $time){ $diff = floatval($highest - $time); $sdiff = $diff * 2; $seconds[] = '+' . $sdiff . 's'; } return $seconds; } ?> If you want it to return the differences in the order you pass them to the function, then I can change that. Hope this helps. Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/64080-assigning-a-time-to-a-number-range/#findComment-319423 Share on other sites More sharing options...
Barand Posted August 9, 2007 Share Posted August 9, 2007 max() will find the highest <?php // Array of timings to pass in to function $times[] = "26"; $times[] = "25.5"; $times[] = "24"; $times[] = "14"; $max = max($times); // get highest val in the array foreach ($times as $k=>$t) { $times[$k] = sprintf('+%ds', ($max-$t)*2); } echo '<pre>', print_r($times, true), '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/64080-assigning-a-time-to-a-number-range/#findComment-319563 Share on other sites More sharing options...
Mutley Posted August 9, 2007 Author Share Posted August 9, 2007 Thanks alot. If I just wanted to print one, say the 3rd highest number, how would I do that? Would I place [1] [2] etc in the [] and print it that way? Quote Link to comment https://forums.phpfreaks.com/topic/64080-assigning-a-time-to-a-number-range/#findComment-319590 Share on other sites More sharing options...
Barand Posted August 9, 2007 Share Posted August 9, 2007 In that case, use Huggies sorted version and echo the 3rd Quote Link to comment https://forums.phpfreaks.com/topic/64080-assigning-a-time-to-a-number-range/#findComment-319597 Share on other sites More sharing options...
Mutley Posted August 9, 2007 Author Share Posted August 9, 2007 I get a call to undefined function error with Huggies, I tried moving the function above where it is called but then it doesn't show any data. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/64080-assigning-a-time-to-a-number-range/#findComment-319604 Share on other sites More sharing options...
Barand Posted August 9, 2007 Share Posted August 9, 2007 worked fine when I ran it Quote Link to comment https://forums.phpfreaks.com/topic/64080-assigning-a-time-to-a-number-range/#findComment-319605 Share on other sites More sharing options...
Mutley Posted August 9, 2007 Author Share Posted August 9, 2007 Thanks, I fixed it, How would one echo the 3rd using that function? Quote Link to comment https://forums.phpfreaks.com/topic/64080-assigning-a-time-to-a-number-range/#findComment-319622 Share on other sites More sharing options...
Barand Posted August 9, 2007 Share Posted August 9, 2007 echo $seconds[2]; Quote Link to comment https://forums.phpfreaks.com/topic/64080-assigning-a-time-to-a-number-range/#findComment-319624 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.