Created
April 30, 2010 23:18
-
-
Save jaywilliams/385876 to your computer and use it in GitHub Desktop.
Convert a comma separated file into an associated array.
This file contains 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 | |
/** | |
* Convert a comma separated file into an associated array. | |
* The first row should contain the array keys. | |
* | |
* Example: | |
* | |
* @param string $filename Path to the CSV file | |
* @param string $delimiter The separator used in the file | |
* @return array | |
* @link http://gist.github.com/385876 | |
* @author Jay Williams <http://myd3.com/> | |
* @copyright Copyright (c) 2010, Jay Williams | |
* @license http://www.opensource.org/licenses/mit-license.php MIT License | |
*/ | |
function csv_to_array($filename='', $delimiter=',') | |
{ | |
if(!file_exists($filename) || !is_readable($filename)) | |
return FALSE; | |
$header = NULL; | |
$data = array(); | |
if (($handle = fopen($filename, 'r')) !== FALSE) | |
{ | |
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) | |
{ | |
if(!$header) | |
$header = $row; | |
else | |
$data[] = array_combine($header, $row); | |
} | |
fclose($handle); | |
} | |
return $data; | |
} | |
/** | |
* Example | |
*/ | |
print_r(csv_to_array('example.csv')); | |
?> |
This file contains 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
name | number | |
---|---|---|
Lorem | 11 | |
ipsum | 22 |
This file contains 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
Array | |
( | |
[0] => Array | |
( | |
[name] => Lorem | |
[number] => 11 | |
) | |
[1] => Array | |
( | |
[name] => ipsum | |
[number] => 22 | |
) | |
) |
Thanks I do know some basic PHP and i have made this task, justmore simple, without the csv file, but this is a little more advanced for me :)
<style> table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%; }td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
</style>
"; echo "Det lavest Tempartur er: ". (min($temp_array)). "°
"; echo " "; $Mtemp = array( "78, 60, 62, 68, 71, 68, 73, 85, 66, 64, 76, 63, 81, 76, 73, 68, 72, 73, 75, 65, 74, 63, 67, 65, 64, 68, 73, 75, 79, 73, 45"); $datesOFM = array('01-01-2011, 02-01-2011, 03-01-2011, 04-01-2011, 05-01-2011, 06-01-2011, 07-01-2011, 08-01-2011, 09-01-2011, 10-01-2011,11-01-2011, 12-01-2011, 13-01-2011, 14-01-2011, 15-01-2011,16-01-2011, 17-01-2011, 18-01-2011, 19-01-2011, 20-01-2011, 21-01-2011, 22-01-2011, 23-01-2011, 24-01-2011, 25-01-2011, 26-01-2011, 27-01-2011, 28-01-2011, 29-01-2011, 30-01-2011, 31-01-2011'); echo " "; foreach($Mtemp as $key => $value) { echo "" . ""; } echo "
Dato | Tempertur |
---|---|
" . $datesOFM[$key] . " | " . $Mtemp[$key] . " |
Thanks @rboatright
As of PHP 8.0, you can set the length to 0 (or null
, or omit it entirely) for the fgetcsv()
function if you don't want to limit the maximum line length.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's many ways you can achieve that, I'd suggest reading through this tutorial, or picking up a good book on the PHP language. With a little time, it's easy to pick up the basics.