简体   繁体   中英

Array sorting for Budget range in PHP

I have a Two PHP arrays as Below

Array 1 - Budget Start

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

Array 2 - Budget End

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

I want to Filter the Budget range which the user is actually looking for.For the above the budget range is 35000 For Budget Start and 40000 for Budget End.

The Budget Start is Calculated by comparing every budget start with every other budget start so that Budget Start should be between Budget Start and Budget End

Budget Start 35000 because

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

Budget End 40000 because

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

Is there some way to resolve this.

Thanks for Reply

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM