You should define what the nearest rounded match is. For 24 it should look the value for 20? I think as I understand what for you use it for 24 it should also be 30 otherwise you should define if round should be defined for a half of weights (first column) or not.
There is sample code that works as I think it should, so for 24 it will also choose 30
PHP file:
<?php
$name = 'product2';
$weight = 24;
$lines = explode("\n", file_get_contents("test.csv"));
$found= '';
$foundCost = false;
foreach ($lines as $line) {
if (strpos($line,$name.',') === 0) {
$found = $line;
break;
}
}
if ($found != '') {
$found = explode(',', $found);
$weights = explode(',',$lines[0]);
$index = false;
for ($i=1; $i<count($weights); ++$i) {
if ($weights[$i] >= $weight) {
$index = $i;
break;
}
}
if ($index !== false) {
$foundCost = $found[$index];
}
}
if ($foundCost !== false) {
echo "found ".$foundCost;
}
CSV file (I assumed there are no space after , as in ordinary CS file):
Weight,10,20,30,40
product1,1,2,3,4
product2,2,4,6,8
product3,3,6,9,12