fork(2) download
  1. <?php
  2. //Собственно, задача, дан массив чисел, найдите 2 наибольших. Решать можно на JS или PHP, кому что роднее.
  3.  
  4. $numbers = array(1, 500, 500, 500, 500, 2, 3, 4, 5, 22, 12, 12, 123, 52, 24);
  5.  
  6. function top2numbers (array $numbers)
  7. {
  8. $top1 = -INF;
  9. $top2 = 0;
  10. foreach ($numbers as $number) {
  11. if ($number > $top1) {
  12. $top2 = $top1;
  13. $top1 = $number;
  14. } elseif (($number > $top2) && ($number != $top1)) {
  15. $top2 = $number;
  16. }
  17. }
  18. return array($top1, $top2);
  19. }
  20.  
  21.  
  22. print_r(top2numbers($numbers));
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
Array
(
    [0] => 500
    [1] => 123
)