Jump to content

search an array


pagegen

Recommended Posts

Hi guys

 

I have an php array like below

 

// array
$array_odds = array(
"1.10" => "1/10",
"1.11" => "1/9",
"1.12" => "1/8",
"1.14" => "1/7"
);

// using the array
echo $array_odds ['1.11']; // will echo in 1/9

 

As we can see in the array there is no 1.13, Is there a way I can pass it 1.13, and it gets me the closest fraction

 

so if I passed it 1.13 it would return the fraction 1 lower which is 1.12 > 1/8

 

 

Thanks guys

 

 

Link to comment
Share on other sites

The only solution I can think of is building a function that would have to iterrate through the values in the array. BUt, you state in your request "it gets me the closest fraction". Are you wanting the closest value (those are decimals not fractions) or are you wanting the closest lower value? For exampl, what if 1.12 didn't exist, but 1.14 did. Would you want the 1.11 value or the 1.14 value?

Link to comment
Share on other sites

The only solution I can think of is building a function that would have to iterrate through the values in the array. BUt, you state in your request "it gets me the closest fraction". Are you wanting the closest value (those are decimals not fractions) or are you wanting the closest lower value? For exampl, what if 1.12 didn't exist, but 1.14 did. Would you want the 1.11 value or the 1.14 value?

 

Hi mate,

 

sorry for my bad example

 

currenly if I

 

echo $array_odds['1.12']; // this will echo 1/8 to the screen

 

but if I go $array_odds['1.13'];

that will echo nuthing

 

so what I want to do is, if its going to return nuthing then get me closest so because 1.13 dont exist in array, return 1.12

 

hope that makes better sence

 

Thank you

Link to comment
Share on other sites

You still didn't answer my question. You said you wanted the "closest" value, but your example returns the closest LOWER value. So, are you always wanting the lower value even if the higher value is closer? HArd to tell since your example has an upper and lower value that are the same difference from the search value.

 

The following function will return the value of the matching key. If no matching key it will return the closest value. but, if the upper and lower keys are the same difference from the search key, it reverts to the lower key

 

[Note: this assumes the array key values are in logical, numerical order]

function getClosestValue($array, $searchKey)
{
    //Check if there is an exact match
    if(array_key_exists($searchKey, $array))
    {
        return $array[$searchKey];
    }
    
    //No exact match, find nearest lowest/highest key values
    $nextLowest = false;
    $nextHighest = false;
    //Determine next lowest and next highest keys
    foreach($array as $key => $value)
    {
        if($key < $searchKey)
        {
            $nextLowest = $key;
        }
        else
        {
            $nextHighest = $key;
            break;
        }
    }
    
    //Determine nearest value
    if($nextHighest!==false && $nextLowest!==false)
    {
        //Neither value is false, return closes key value
        $closestDiff = min(($searchKey-$nextLowest), ($nextHighest-$searchKey));
        return $array[(($closestDiff==($searchKey-$nextLowest))?$nextLowest:$nextHighest)];
    }
    if($nextLowest===false XOR $nextLowest===false)
    {
        //One value is false
        return ($nextLowest!==false) ? array[$nextLowest] : array[$nextHighest];
    }
    //Both values are false
    return false;
}

Link to comment
Share on other sites

You still didn't answer my question. You said you wanted the "closest" value, but your example returns the closest LOWER value. So, are you always wanting the lower value even if the higher value is closer? HArd to tell since your example has an upper and lower value that are the same difference from the search value.

 

The following function will return the value of the matching key. If no matching key it will return the closest value. but, if the upper and lower keys are the same difference from the search key, it reverts to the lower key

 

[Note: this assumes the array key values are in logical, numerical order]

function getClosestValue($array, $searchKey)
{
    //Check if there is an exact match
    if(array_key_exists($searchKey, $array))
    {
        return $array[$searchKey];
    }
    
    //No exact match, find nearest lowest/highest key values
    $nextLowest = false;
    $nextHighest = false;
    //Determine next lowest and next highest keys
    foreach($array as $key => $value)
    {
        if($key < $searchKey)
        {
            $nextLowest = $key;
        }
        else
        {
            $nextHighest = $key;
            break;
        }
    }
    
    //Determine nearest value
    if($nextHighest!==false && $nextLowest!==false)
    {
        //Neither value is false, return closes key value
        $closestDiff = min(($searchKey-$nextLowest), ($nextHighest-$searchKey));
        return $array[(($closestDiff==($searchKey-$nextLowest))?$nextLowest:$nextHighest)];
    }
    if($nextLowest===false XOR $nextLowest===false)
    {
        //One value is false
        return ($nextLowest!==false) ? array[$nextLowest] : array[$nextHighest];
    }
    //Both values are false
    return false;
}

 

Hi mate, thanks for the function I will test it

 

but yes in all cases I want to pass a decimal ('1.12') and echo the fraction value for that ('1/8')

Link to comment
Share on other sites

but yes in all cases I want to pass a decimal ('1.12') and echo the fraction value for that ('1/8')

 

Huh? Let me give you an example.

 

Let's say your array contains the following:

$array_odds = array(
    "1.10" => "1/10",
    "1.11" => "1/9",
    "1.14" => "1/7"
);

 

What value would you want returned for 1.13? Do you want "1/9", which is the closest lower value? Or, do you want "1/7" which is the closest absolute value?

Link to comment
Share on other sites

but yes in all cases I want to pass a decimal ('1.12') and echo the fraction value for that ('1/8')

 

Huh? Let me give you an example.

 

Let's say your array contains the following:

$array_odds = array(
    "1.10" => "1/10",
    "1.11" => "1/9",
    "1.14" => "1/7"
);

 

What value would you want returned for 1.13? Do you want "1/9", which is the closest lower value? Or, do you want "1/7" which is the closest absolute value?

 

 

Hi I would like the "1/9" which is the closest lower value

 

Thank you

Link to comment
Share on other sites

Hi I would like the "1/9" which is the closest lower value

 

Then, the function can be simplified to this

function getClosestValue($array, $searchKey)
{
    //Check if there is an exact match
    if(array_key_exists($searchKey, $array))
    {
        return $array[$searchKey];
    }
    
    //No exact match, find next lowest key value
    $nextLowest = false;
    //Determine next lowest keys
    foreach($array as $key => $value)
    {
        if($key < $searchKey)
        {
            $nextLowest = $key;
        }
        else
        {
            break;
        }
    }
    
    //Return next lowest value, if exists
    return ($nextLowest!==false) ? array[$nextLowest] : false;
}

Link to comment
Share on other sites

Hey mate,

 

tried the function

 

but getting this error

Parse error: syntax error, unexpected '[', expecting '(' on line 26

 

line 26 is

return ($nextLowest!==false) ? array[$nextLowest] : false;

 

the code seems correct though

 

 

 

EDIT: changed the return to this

return $nextLowest;

and it works?

 

Link to comment
Share on other sites

The only other issue is, some times its returning the decimal which is strange

 

No, it makes perfect sense. The original code was supposed to return the value of the array element. Your change makes it return the key of the element.

 

As my signature states I do not always test my code. The error in what I supplied was a very simple error. I simply left off the dollar sign on the array variable. Change the last line to this:

return ($nextLowest!==false) ? $array[$nextLowest] : false;

 

The function will then return the value as intended. The false is there in the event that there is no key lower than the value being searched for.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.