Skip to content

Instantly share code, notes, and snippets.

@erfg12
Last active April 9, 2019 17:59
Show Gist options
  • Save erfg12/95e524a88d585a125d6df6673d573e80 to your computer and use it in GitHub Desktop.
Save erfg12/95e524a88d585a125d6df6673d573e80 to your computer and use it in GitHub Desktop.
Cross reference a CSV (excel) file (column by row) for a value
<?PHP
// Get value from matrix chart. H = first column going down, W = first row across
function getValueFromCSV($h, $w) {
$v = 0;
$k = 0;
$row = 0;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 100, ",")) !== FALSE) {
$key = array_search($w, $data, true);
if ($key != null && $row == 0)
$k = $key;
if (in_array ($h,$data)){
$v = $data[$k];
}
}
$row++;
}
fclose($handle);
return $v;
}
if ($v = getValueFromCSV("test1","20"))
echo "FOUND IT - ".$v;
else
echo "DIDN'T FIND IT";
?>
5 10 15 20
test1 6 11 16 21
test2 7 12 17 22
test3 8 13 18 23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment