title: how to find highest and second highest number in an array without using max function
body: <p>I already have solution. But i think it will be more optimizable. So please provide me a solution for it. And remember that don't use predefined function of php. Like max() function.
  i know there are so many ways to find it but i want best and better solution. Because my array contains more than 1 lakh records and it's taking lot of time. Or sometime site will be down.</p>
  
  <p>My code :</p>
  
  <pre><code>&lt;?php 
  
  $array = array('1', '15', '2','10',4);
  
  echo "&lt;pre&gt;";
  print_r($array);
  echo "&lt;pre&gt;";
  $max = 0;
  $s_max=0;
  
  for($i=0; $i&lt;count($array); $i++)
  {
      $a = $array[$i];
      $tmax = $max;
      $ts_max = $s_max;
      if($a &gt; $tmax &amp;&amp; $a &gt; $ts_max)
      {
          $max = $a;
          if($tmax &gt; $ts_max) {
              $s_max = $tmax;
          } else {
              $s_max = $ts_max;
          }
      } else if($tmax &gt; $a &amp;&amp; $tmax &gt; $ts_max)
      {
          $max = $tmax;
          if($a &gt; $ts_max) {
              $s_max = $a;
          } else {
              $s_max = $ts_max;
          }
      } else if($ts_max &gt; $a &amp;&amp; $ts_max &gt; $tmax)
      {
          $max = $ts_max;
          if($a &gt; $tmax)
          {
              $s_max = $a;
          } else {
              $s_max = $tmax;
          }
      }
  }
  echo "Max=".$max;
      echo "&lt;br /&gt;";
      echo "S_max=".$s_max;
      echo "&lt;br /&gt;";
  
  ?&gt;
  </code></pre>