0

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.

4
  • can you show me an example? Commented Jan 23, 2014 at 14:00
  • echo "[] ". $intarray[0]. " - " .$intarray[4][0]."99"; However what do you want to do exactly? On the first row to print the range from 1st to fifth element, or to count how many elements are from the range 300 - 699 ? Commented Jan 23, 2014 at 14:01
  • Why is your int array full of strings instead of integers? Commented Jan 23, 2014 at 14:09
  • sorry about that right now I am editing the post. ignore the " Commented Jan 23, 2014 at 14:10

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

make sense. but I don't think it can be done with switching. Because numbers may not the same. How to put limits to them like 700 - 999 ?
1

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.