title: How to find the mode of an array in PHP
body: <p>I have an array that has been sorted from low to high which has over 260k values in. I have found out the mean(average) and median of the array just need to find out the mode?</p>
  
  <p>I cannot use any mathematical functions that PHP has, it has to be all done manually. </p>
  
  <p>I would like it so there could be just one value that is the mode but then there can be multiple values that can be the mode. I also need to be able to record the number of times that the value is stored. For example the number 51 appears 6 times so I can print both values.</p>
  
  <p>This is my code so far:</p>
  
  <pre><code>$amountRecords = 0;
  $totalValue = 0;
  $valueArray = array();
  
  // reads in csv file
  $handle = fopen('Task1-DataForMeanMedianMode.csv', 'r');
  // to skip the header names/values
  fgetcsv($handle);
  
  // creates array containing variables of csv file in ascending order
  while(($row = fgetcsv($handle, "\r")) != FALSE)
  {
  
      // if amountRecords equals 0
      if($amountRecords == 0)
      {
  
          // adds value from csv to array
          $valueArray[] = $row[1];
  
      } // else amountRecords does not equal 0
      else 
      {
  
          // if the value in array location before is greater than the current value from the csv file
          if($valueArray[$amountRecords - 1] &gt; $row[1])
          {
  
               // the current array location becomes the one in the location before
               $valueArray[] = $valueArray[$amountRecords - 1];
               // add the value from the csv to the location before
               $valueArray[$amountRecords - 1] = $row[1];
  
           } // if the value in the location before is not greater than the current value in the csv file
           else 
           {
  
               // adds value from csv to array
               $valueArray[] = $row[1];
  
           }
  
      }
  
      // calculates the total value of the values in csv file
      $totalValue = $totalValue + $row[1];
      // calculates the number of values in the csv file
      $amountRecords++;
  
  }    
  
  // calculate average value of payments
  $averageValue = $totalValue / $amountRecords;
  // limit integer to 2 decimal place
  $average = number_format($averageValue,2,'.','');
  
  // finds middle value
  $middle = floor(($amountRecords / 2) - 1);
  
  // calculates the median value
  // if array size is even
  if($amountRecords % 2 == 0)
  {
  
      // calculates median
      $median = $valueArray[$middle];
  
  } 
  else // if array size is odd
  {
  
      // calculate low and high values
      $low = $valueArray[$middle];
      $high = $valueArray[$middle + 1];
      // calculates median
      $median = (($low + $high) / 2);
  
  }
  
  // works out mode
  // creates array count
  $count = array();
  // for each value in the valueArray
  foreach( $valueArray as $value )
  {
  
      if( isset( $count[$value] ))
      {
  
          $count[$value]++;
  
      }
      else
      {
  
          $count[$value] = 1;
  
      }
  
  }
  
  $mostCommon = "";
  $iter = 0;
  
  foreach( $count as $k =&gt; $v )
  {
  
       if( $v &gt; $iter )
       {
  
           $mostCommon = $k;
           $iter = $v;
  
       }
  
  }
  
  $modeArray = array( "mode" =&gt; $mostCommon , "count" =&gt; $iter );
  </code></pre>