Search Value from range of an array

Ketan Lathiya

I am trying to find value from different ranges.

e.g. I have one value is 5 and now i have different ranges like [0,100], [101-500] and [501-1000], so how can I conclude that my value 5 is in range of [0,100] using php.

I have tried in_array but it is considering only one range, like below:

$arr = array("5", "25", "100", "200", "500");
foreach($arr as $val){
$i = $val; // Dynamic value   
if (in_array($i, range(0,10))) {
echo 'Your number is in between of range array';
}
}

Here i can take only one range, I need to check different ranges there. how can I do that ? range value[0,100],[101,500] and [501,1000] are fixed.

Mobasher Fasihy

This may do the action you want.

$range[1] = array (0,101,501);
$range[2] = array (100,500,1000);
$testNumber=7;
$count = count($range);
for ($i = 0; $i <= $count; $i++) 
{
    if ($testNumber>=$range[1][$i])
    {
        if  ($testNumber<=$range[2][$i])
        {
            echo $testNumber." is in range of [".$range[1][$i].' , '.$range[2][$i]."]";
            break;
        }
    }
    else if($testNumber<$range[1][$i])
    {
       echo "not found";break;
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Array()= range()。value

来自分类Dev

Excel: Get max value from variable range

来自分类Dev

Remove value from array if value is a substring of another value

来自分类Dev

PHP get a value from array index

来自分类Dev

Range(“”)。Value =“ Percentage%”错误

来自分类Dev

Range().value="=(if) ... " 在 Excel 中

来自分类Dev

Is it possible to efficiently create a balanced binary search tree from an unsorted array without sorting the array?

来自分类Dev

How can I search a range of lines in python?

来自分类Dev

递归array_search

来自分类Dev

Range1.Value = Range2.Value 未填充

来自分类Dev

create a paired value cell array from existing vectors

来自分类Dev

How do I get a value from an associative array using Swift

来自分类Dev

VBA中Range()的.Value属性

来自分类Dev

Ruby regex search and replace array

来自分类Dev

Replace specific letter to a range numbers and search in database using php

来自分类Dev

Distribute values over value-range tiers

来自分类Dev

Range.Value 不起作用

来自分类Dev

SQL column value null, search filter mistaken

来自分类Dev

Read json file to search key value in python

来自分类Dev

如何访问`$ scope.search.value`?

来自分类Dev

array_search并销毁逻辑错误

来自分类Dev

array_search返回NULL /空白

来自分类Dev

array_search递归函数php

来自分类Dev

返回与array_search相反的值

来自分类Dev

on search getting array out of index bound exception

来自分类Dev

array_search返回错误的密钥

来自分类Dev

array_search返回比较差异

来自分类Dev

Array_Search多个相同的元素

来自分类Dev

array_search返回NULL /空白