Pergunta

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.

Foi útil?

Solução

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;

}

Outras dicas

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;


}

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top