0
I have to find out if the value (5.55 for example) exists in a matrix and know in which range of that matrix this, and bring me the id of the range.
I had taken a test and it seemed to be working and when it entered a value 0 (zero) already broke my logic, then I made a change and this working, but then I do not know if there is any better way to do.
<?php
$precisao = 0.000001;
$matriz = array(
array('id'=> 1 , 'vl_inicial' => 0.00, 'vl_final', 19.55),
array('id'=> 2 , 'vl_inicial' => 19.56, 'vl_final', 28.23),
array('id'=> 3 , 'vl_inicial' => 30.00, 'vl_final', 100.00)
);
$valor = 5.55;
foreach($matriz as $m) {
if( in_array($valor, range($m['vl_inicial'], $m['vl_final'])) ||
(($valor - $m['vl_inicial']) > $precisao) AND (($valor - $m['vl_final']) <= $precisao) )
{
return $m['id'];
}
}
return FALSE;
Why not just make two comparisons? has an array of values and needs to know if the number exists in the array, or only if it is within an interval between two numbers?
– Sergio
@Sergio then, I made some changes to my question which I now hope is clearer. Thank you
– Marcelo Diniz