Sorting an array for a budget range in PHP

I have two PHP arrays below

Array 1 - Budget Start

Array
(
    [0] => 25000
    [1] => 30000
    [2] => 35000
    [3] => 15900
)

      

Array 2 - End of budget

Array
(
    [0] => 40000
    [1] => 50000
    [2] => 60000
    [3] => 55000
)

      

I want to filter the budget range that the user is actually looking for. For the above budget range, 35000 for start budget and 40,000 for end budget.

The initial budget is calculated by comparing each budget start to every other budget start so that the initial budget starts at the start of the budget and the end of the budget

Budget Start 35000 because

25000 <= 35000 < 40000
30000 <= 35000 < 50000
35000 <= 35000 < 60000
15900 <= 35000 < 55000

      

Budget end 40,000 because

25000 < 40000 <= 40000
30000 < 40000 <= 50000
35000 < 40000 <= 60000
15900 < 40000 <= 55000

      

Is there any way to solve this problem.

thanks for the answer

+3


source to share


1 answer


<?php
$start = Array(25000,30000,35000,15900);

$end = Array(40000,50000,60000,55000);


foreach($start as $val){
    $cnt = 0;
    for($i=0;$i<count($start); $i++){
        if($start[$i] <= $val && $val < $end[$i]){
            $cnt++;
        }
        if($cnt == count($start)){
            $start_budget = $val;
        }
    }
}

foreach($end as $val){
    $cnt = 0;
    for($i=0;$i<count($end); $i++){
        if($start[$i] < $val && $val <= $end[$i]){
            $cnt++;
        }
        if($cnt == count($end)){
            $end_budget = $val;
        }
    }
}

echo $start_budget;
echo "<br>";
echo $end_budget;
?>

      



+1


source







All Articles