<?php
/**
* CSV File to Assiciative Array ( Simply Using fgetcsv() )
* &&
* CSV String to Associative Array ( Using tmpfile() )
*/
error_reporting(E_ALL);
echo '<pre>'.var_export( CSV::file2Array('csv.csv') , true ).'</pre>';
echo '<pre>'.var_export( CSV::str2Array( "f1,f2,f3\naa,\"bb,bb\",cc\nxx,yy,zz" ) , true ).'</pre>';
class CSV
{
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//General Utility Functions Start
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
/**
* Utility Function to make array from csv file, Array keys are same as csv column name.
*
* Function internaly call fgetcsv to perform it operation
* @access public
* @return Array Return array is associative array , Whose keys are identical to csv column names )
* @param string valid file path
* @param character defalut value ','
* @param character default value '"'
* @param character defalut '\' currently ignored
*/
public static function file2Array( $csv_path, $delimiter=',', $enclosure='"', $escape='\\' )
{
//return associative array from csv file
//assumption $csvstr containcsv data, first line containing column name
// $csvstr = "f1,f2,f3\naa,\"bb,bb\",cc\nxx,yy,zz";
// Array( [0] => Array( [f1] => aa [f2] => bb,bb [f3] => cc )
// [1] => Array( [f1] => xx [f2] => yy [f3] => zz ) )
$handle = @fopen($csv_path, "r");
#var_dump( $csv_path, $handle ); die();
$ret = Array();
if ( false !== $handle)
{
//retrive columns
$cols = @fgetcsv($handle, 1000, $delimiter, $enclosure);
//make all value to lower case
foreach($cols as $k=>$v)
{
$cols[$k]= strtolower(trim($v));
}
while( ($data = @fgetcsv($handle, 1000)) !== FALSE)
{
if ( '' != trim($data[0]) )//for ignoring empty lines
{
//IMP to ignore empty lines
$ret[] = array_combine($cols, $data);
}
}
@fclose($handle);
}
return $ret;
}//END_OF_csvFile2Array
/**
* Utility Function to,Convert csv string to array with keys are same as column name
*
* Function internaly call fgetcsv to perform it operation, <br>
* for this it create temporery file usign tempfile() wich will be deleted on fclose().
* @access public
* @return Array Return array is associative array , Whose keys are identical to csv column names )
* @param string valid file path
* @param character defalut value ','
* @param character default value '"'
* @param character defalut '\' currently ignored
*/
public static function str2Array( $csv_str, $delimiter=',', $enclosure='"', $escape='\\' )
{
//return associative array from csv data string
//assumption $csvstr containcsv data, first line containing column name
// $csvstr = "f1,f2,f3\naa,\"bb,bb\",cc\nxx,yy,zz";
// Array( [0] => Array( [f1] => aa [f2] => bb,bb [f3] => cc )
// [1] => Array( [f1] => xx [f2] => yy [f3] => zz ) )
$handle = tmpfile();
$ret = Array();
if ( false !== $handle)
{
//Write csv data to string
@fwrite($handle, $csv_str);
@fseek($handle, 0);//Move pointer to start
//retrive columns
$cols = @fgetcsv($handle, 1000, $delimiter, $enclosure);
//make all value to lower case
foreach($cols as $k=>$v)
{
$cols[$k]= strtolower(trim($v));
}
while( ($data = @fgetcsv($handle, 1000, $delimiter, $enclosure)) !== FALSE)
{
if ( '' != trim($data[0]) )
{
//IMP to ignore empty lines
$ret[] = array_combine($cols, $data);
}
}
@fclose($handle);
}
return $ret;
}//END_OF_csvStr2Array
public static function array2Str( $arr ){ die('CSV::array2Str() yet not implemented '); }
public static function array2File( $arr , $path){ die('CSV::array2File() yet not implemented '); }
}
?>
PHP MP3 Tag Read Write
18 years ago