Per ora ho fatto così
codice:
/* Price */
$pricelist = array();
foreach ($products as $product) {
$pricelist[] = $product['price'];
}
$price_ranges = array();
$done = 0;
foreach ($pricelist as $price) {
$base = pow(10, strlen(floor($price))-1);
$min = (int)(floor($price/$base)*$base);
$max = (int)(ceil($price/$base)*$base);
if ($price >= 1 && $price < 1000) {
$max = $max == $min ? $min+$base : $max; // Ensurese there is no "20 to 20" like range
}
if ($price >= 1000) { // Different approach for higher prices
$avg = ($min+$max)/2;
if ($price < $avg) {
$max = $avg;
} else {
$min = $avg;
}
$max = $max == $min ? $min+($base/2) : $max;
}
$range = $min .','.$max;
if (!array_key_exists($min, $price_ranges)) {
$price_ranges[$min]['from'] = $min;
$price_ranges[$min]['to'] = $max;
$price_ranges[$min]['count'] = 1;
} else {
$price_ranges[$min]['count']++;
}
} // End foreach pricelist