2

After looking for a built in function in php I couldn't find a similar function to Excel's vlookup function.

I need a function that takes in an array and a lookup value and return the required info. So for example:

<?php
$baseCalculationPrice = [
    0 => 50,        //for values <=500 but >0
    500 => 18,      //for values <=3000 but >500
    3000 => 15,     //for values <=5000 but >3000
    5000 => 14,     //for values >5000 
];

//Examples
    $numPages = 499;
    echo vlookup($numPages,$baseCalculationPrice); //should output 50
    $numPages = 500;
    echo vlookup($numPages,$baseCalculationPrice); //should output 50
    $numPages = 501;
    echo vlookup($numPages,$baseCalculationPrice); //should output 18
    $numPages = 3000;
    echo vlookup($numPages,$baseCalculationPrice); //should output 18
    $numPages = 3001;
    echo vlookup($numPages,$baseCalculationPrice); //should output 15
    $numPages = 5000;
    echo vlookup($numPages,$baseCalculationPrice); //should output 15
    $numPages = 5001;
    echo vlookup($numPages,$baseCalculationPrice); //should output 14

function vlookup($value,$array){
    //magic code

    return ....;
}
?>

I'm stuck even with the logic behind such a function, so any help would be great - thanks.

3
  • In my research I came across this stackoverflow.com/questions/16234992/… . However as it needs to work for values in between I couldn't find use of it - maybe others might Commented May 11, 2014 at 2:24
  • 1
    There is nothing like that in PHP. The closest would be array_searhc(), and it only does exact matches. YOu'll have to sort the array, then loop to find the nearest keys. Commented May 11, 2014 at 2:34
  • @MarcB sorry do you mean array_search() ? Commented May 11, 2014 at 3:09

4 Answers 4

3
function vlookup($lookupValue,$array){

    $result;

    //test each set against the $lookupValue variable,
    //and set/reset the $result value

    foreach($array as $key => $value)
    {
        if($lookupValue > $key)
        {
        $result = $value;
        }
    }

    return $result;

}
Sign up to request clarification or add additional context in comments.

2 Comments

you did say "I need a function" right? 'cus this is the best I can think of, there's nothing native like that in PHP
What if the key value is not an integer? for example 44.8 => 7
3
function testDeductionRate(){echo $this->deductionRate('ks',300);//zone and time are parameter for deductionRate() }

//deduction will be acted as vlookup (rangeLookup true)
function deductionRate($zone,$time){
$actualRate = 0;
$allRate = array(
    'tg'=>array(0=>0,20=>200,30=>300,40=>400,50=>500), 
    'ks'=>array(0=>0,20=>100,30=>200,40=>300,50=>400), 
    'pc'=>array(0=>0,20=>50,30=>100,40=>200,50=>300) 
);
if(isset($allRate[$zone])){
    $rate = $allRate[$zone];
    foreach($rate as $key=>$val){
        if($time>=$key) $actualRate = $val; 
    }
}else{
    return -1; //-1 means error
}
return $actualRate;


}

Comments

1

Try this if you would like to query on multi dimension associative array:

 function vlookup($lookup_vakue, $lookup_array, $lookup_column, $result_column)
    {
        foreach ($lookup_array as $item) {
                if ($item[$lookup_column] == $lookup_vakue) {
                    return $item[$result_column];
                }
        }
        return false;
    }

Sample Data

$data = 
[ 
   ['id'=>1,'price'=>100],
   ['id'=>2,'price'=>200],
   ['id'=>3,'price'=>300]
];

Query Option

$result = vlookup('2',$data, 'id','price');

Result:

200

Comments

0
$getbase= function($amount) use ($baseCalculationPrice)
    {
        return (end(array_filter(
        $baseCalculationPrice,function ($key) use ($amount) 
            {
              return $key < $amount;
            },
        ARRAY_FILTER_USE_KEY
        )));
     };

amount 150 result 50 amount 1500 result 18 amount 25000 result 14

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.