Last active
April 9, 2019 17:59
-
-
Save erfg12/95e524a88d585a125d6df6673d573e80 to your computer and use it in GitHub Desktop.
Cross reference a CSV (excel) file (column by row) for a value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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"; | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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