PHP检查其他int范围内的int范围

时间:2017-06-14 20:10:51

标签: php

让我们说我们有一辆价格在1000美元到5000美元之间的汽车。现在,当用户搜索开始的汽车时:

  1. 从999 $到1001 $
  2. 从1001 $到4999 $
  3. 从$ 4999到$ 5001
  4. 应该显示这辆车。 到目前为止,我没有成功。 尝试:

    $rangeMin = (int)$searchedTerms->rangemin;
        //substract 10% from min range
        $rangeMin = (int)($rangeMin - ($rangeMin / 10));
    
        $rangeMax = (int)$searchedTerms->rangemax;
        //add 10% to max range
        $rangeMax = (int)($rangeMax + ($rangeMax / 10));
    
        for($i=0; $i< count($result); $i++){
            if( 
            in_array( (int)$result[$i]['price_min'], range($rangeMin, $rangeMax,1)) || 
            in_array( (int)$result[$i]['price_max'], range($rangeMin, $rangeMax,1) ) || 
            in_array( $rangeMin, range((int)$result[$i]['price_min'], (int)$result[$i]['price_max']),1 ) || 
            in_array( $rangeMax, range((int)$result[$i]['price_min'], (int)$result[$i]['price_max']),1 ) ) {
                array_push($final, $result[$i]);
            }
        }
    

2 个答案:

答案 0 :(得分:0)

简单的if语句应该有效:

return
    ($search['low'] >= $car['low'] && $search['low'] <= $car['high']) 
    || ($search['high'] >= $car['low'] && $search['high'] <= $car['high']);

见下面的测试计划here:     

$car = ['low' => 1000, 'high' => 5000];

$search_positive_tests = [
    ['low' => 999, 'high' => 1001],
    ['low' => 1001, 'high' => 4999],
    ['low' => 4999, 'high' => 5001]
];

$search_negative_tests = [
    ['low' => 998, 'high' => 999],
    ['low' => 5001, 'high' => 6000]
];

function is_match($car, $search)
{
    return
        ($search['low'] >= $car['low'] && $search['low'] <= $car['high']) 
        || ($search['high'] >= $car['low'] && $search['high'] <= $car['high']);
}


foreach ( $search_positive_tests as $search )
{
    if ( !is_match($car, $search) )
        throw new Exception('(' . $car['low'] . ' => ' . $car['high'] . ') should have matched (' . $search['low'] . ' => ' . $search['high'] . ')');
}

foreach ( $search_negative_tests as $search )
{
    if ( is_match($car, $search) )
        throw new Exception($car['low'] . ' => ' . $car['high'] . ' should not have matched (' . $search['low'] . ' => ' . $search['high'] . ')');
}

答案 1 :(得分:0)

以下内容应该可以解决问题:

header{
    width:100%;
    background-color: #607D8B; 
    color:#8BC34A; 
    font-size:200%;
    padding: 15px;
    position: fixed;
    z-index: 999;
    
}

article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}

.image{
    width: 100%;
}

body {

height: 1200px;
}