Reputation: 510
Let's say I have an array like this :
$intarray = array("300","350","399","650","625","738","983","1200","1050");
how can I display this array to user like this :
Price
_________________
[] 300 - 699 (5)
[] 700 - 999 (2)
[] 1000 - 1500 (2)
Details : as in the example I wanted to show the user not the whole elements but option to select between them by giving limits low and max limits. So if user select 300 - 699, page display the results between 300-699
That $intarray is generated dynamically so some code must handle the splitting. Array can have more elements. What I want is divide the numbers like 5 range options to show the user.
Upvotes: 0
Views: 91
Reputation: 730
You can do a function that prints and counts the numbers in your range
public function printInRanges($startRange, $endRange)
{
$count = 0;
foreach($element in $array)
{
if($element>=$startRange && $element<=$endRange)
count++;
}
echo $startRange."-".$endRange."(".$count.")";
}
And than you can call this function with whatever ranges you want
Last Edit
if you want to do this for all your array, get your first element value (array[0]) and last element value, and call the function from a loop
$startValue = array[0];
while($startValue + 500 < $endValue)// 500 being the value between ranges like 0-500,500-1000,1000-1500
{
printInRanges($startValue ,$startValue +500);
$startValue+=500;
}
Upvotes: 1
Reputation: 7034
Having in mind my comment, I assume you want to count particular ranges.
<?php
$intarray = array("300","350","399","650","625","738","983","1200","1050");
//echo "[] ". $intarray[0]. " - " .$intarray[4][0]."99"; # that was in my comment
$ranges = array(
0 => array(
'min' => 300,
'max' => 699
),
1 => array(
'min' => 700,
'max' => 999
),
2 => array(
'min' => 1000,
'max' => 1500
)
);
foreach ($intarray as $val) {
for ($i = 0; $i < count($ranges); $i++){
if ($val >= $ranges[$i]['min'] && $val <= $ranges[$i]['max']) {
$range_values[$i][] = $val;
}
}
}
var_dump($range_values);
array (size=3)
0 =>
array (size=5)
0 => string '300' (length=3)
1 => string '350' (length=3)
2 => string '399' (length=3)
3 => string '650' (length=3)
4 => string '625' (length=3)
1 =>
array (size=2)
0 => string '738' (length=3)
1 => string '983' (length=3)
2 =>
array (size=2)
0 => string '1200' (length=4)
1 => string '1050' (length=4)
You can use count()
in order to display the thing in the brackets
for ($i = 0; $i < count($ranges); $i++) {
$max = max($range_values[$i]);
echo "[] " . min($range_values[$i]) . " - " . $max[0]."99" . " (". count($range_values[$i]).")" . "<br />";
}
[] 300 - 699 (5)
[] 738 - 999 (2)
[] 1050 - 199 (2)
Or just display the ranges (as it was the desired output?) and count($ranges_values) current iteration
for ($i = 0; $i < count($ranges); $i++) {
echo "[] " . $ranges[$i]['min'] . " - " . $ranges[$i]['max'] . " (" . count($range_values[$i]) . ")" . "<br />";
}
[] 300 - 699 (5)
[] 700 - 999 (2)
[] 1000 - 1500 (2)
Upvotes: 1
Reputation: 6344
Try
$intarray = array("300","350","399","650","625","738","983","1200","1050");
sort($intarray);
$result=array();
foreach($intarray as $key=>$val){
switch($val){
case ($val > 300 && $val < 699):
$result[0][] = $val;
break;
case ($val > 700 && $val < 999):
$result[1][] = $val;
break;
case ($val > 1000 && $val < 1500):
$result[2][] = $val;
break;
}
}
demo here
Upvotes: 1