Math problem - Array of decimal - create price filter

  • Thread starter Thread starter Izi
  • Start date Start date

Izi

Izi

Soldato
Joined
9 Dec 2007
Posts
2,718
I have a list/array of prices. From this i need to create a filter like so:

£0-25
£26-50
£51-99

etc

say I have 1000 prices in the array, how would you go about factoring the prices in to filters?
 
What language are you working in?

Either way it'd be something along the lines of creating 3 new arrays, one for each price filter. Then looping through the array of all values and testing the value against the criteria and adding it to the relevant filter array.
 
loop through each and i would then either add each to a further list to identify the banding or do an increment on a integer (switch case would probably do it)

you would then be able to use the count attribute to see the values in each band
 
php example (not actually tested..)

PHP:
$filters = array(-1 => 26, 26 => 51, 51 => 101);

function filterPrices ($filters, $prices)
{
  $filteredPrices = array();
  foreach ($filters  as  $min => $max)
  $key = "{$min}-{$max}";
  $filteredPrices[$key] = array();
  foreach($prices as $price)
  {
    if ( ($price > $min) && ($price < $max) )
    {
      $filteredPrices[$key][] = $price;
    }
  }
  return $filteredPrices;
}
 
Last edited:
cheers folks.

i decided in the end to let the admin enter price ranges, and only show price filters if the search results contained items which fit in the specified ranges.
 
Back
Top Bottom