How to fetch a value between a past range

Asked

Viewed 1,170 times

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;
  • 2

    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 then, I made some changes to my question which I now hope is clearer. Thank you

1 answer

3


Of PHP documentation, more specifically in the description of the parameter $step:

Description

array range ( mixed $low , mixed $high [, number $step ] )

Create an array containing a range of elements

Parameters [...]

step

If the step parameter is specified, it will be used as the increment between the elements of the sequence. step must be a positive integer. If not specified, step will have value equal to 1.

That is, you are using the function range and is expecting a different result.

To check if the value is within a range, a simple if two-pointed.

<?php

$valor = 5.55;

$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)
);

foreach($matriz as $intervalo) {

    if( ($valor >=  $intervalo['vl_inicial']) && ($valor <= $intervalo['vl_final']) )
    {
        // return $m['id'];
    }            
}
  • So I had tried to summarize a little of my problem and with that it would be exactly your same answer, as simple as possible, but I need to find the value in a range but within a matrix. I’m sorry for misinterpreting me.

  • @Marcelodiniz edited, basically the answer is the same: the range is not the solution.

  • A simple thing I do not know why I was complicated right!? Thank you

  • Sometimes we just need a look outside the problem we want to solve :)

  • Well that’s right... that’s why I decided to post my problem here.. the way I showed it was working, but for me to understand it was complicated.

  • And I made a change to your answer, because the equality test needed to be done not only in the final value but also in the initial value.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.