我从导入CSV文件获取这些数据。在CSV文件中,我的第一列是年份,所有其他列都是Make。我正在使用 csvreader 库来解析 CSV 文件中的数据。
我的 CSV 浏览器库。
class CI_Csvreader {
var $fields; /** columns names retrieved after parsing */
var $separator = ','; /** separator used to explode each line */
/**
* Parse a text containing CSV formatted data.
*
* @access public
* @param string
* @return array
*/
function parse_text($p_Text) {
$lines = explode("n", $p_Text);
return $this->parse_lines($lines);
}
/**
* Parse a file containing CSV formatted data.
*
* @access public
* @param string
* @return array
*/
function parse_file($p_Filepath) {
$lines = file($p_Filepath);
return $this->parse_lines($lines);
}
/**
* Parse an array of text lines containing CSV formatted data.
*
* @access public
* @param array
* @return array
*/
function parse_lines($p_CSVLines) {
$content = FALSE;
foreach( $p_CSVLines as $line_num => $line ) {
if( $line != '' ) { // skip empty lines
$elements = explode($this->separator, $line);
if( !is_array($content) ) { // the first line contains fields names
$this->fields = $elements;
$content = array();
} else {
$item = array();
foreach( $this->fields as $id => $field ) {
if( isset($elements[$id]) ) {
$item[$field] = $elements[$id];
}
}
$content[] = $item;
}
}
}
return $content;
}
我的 CSV 文件数据 =>
Years Make Make Make
2001 Acura Honda Toyota
2002 Acura Honda
2003 Acura Toyota
2004
在上面的文件中,年份和在Excel/CSV工作表中创建数据可以稍后更改。
我的输出是一个数组.=>
Array
(
[0] => Array
(
[Years] => 2001
[Make] => Acura
[Make] => Honda
[Make] => Toyota
)
[1] => Array
(
[Years] => 2002
[Make] => Acura
[Make] => Honda
[Make] =>
)
[2] => Array
(
[Years] => 2003
[Make] => Acura
[Make] =>
[Make] => Toyota
)
[3] => Array
(
[Years] => 2004
[Make] =>
[Make] =>
[Make] =>
)
)
我想要这样的结果数组 =>我想保留空值。
Array
(
[0] => Array
(
[Years] => 2001
[Make] => Array(
[0]=>Acura
[1]=>Honda
[2]=>Toyota
)
)
[1] => Array
(
[Years] => 2002
[Make] => Array(
[0]=>Acura
[1]=>Honda
[2]=>
)
)
[2] => Array
(
[Years] => 2003
[Make] => Array(
[0]=>Acura
[1]=>
[2]=>Toyota
)
)
[3] => Array
(
[Years] => 2004
[Make] => Array(
[0]=>
[1]=>
[2]=>
)
)
)
另外,请告诉我如何在没有空值的情况下获得结果。
如果有任何其他方法可以从我想要的格式的CSV文件中获取数据,那就可以了。
谁能帮帮我。谢谢。
您可以使用此函数解析 CSV 文件
function CSV_parse($string='', $has_header=true, $row_delimiter=PHP_EOL, $delimiter = "," , $enclosure = '"' , $escape = "\" )
{
$rows = array_filter(explode($row_delimiter, $string));
$firstline = true;
$data = array();
foreach($rows as $row)
{
if($firstline && $has_header)
{
$firstline=false;
continue;
}
$row = str_getcsv ($row, $delimiter, $enclosure , $escape);
$dtrow=array('Years'=>$row[0], 'Makes'=>array());
for($i=1;$i<count($row);$i++)
{
$make=trim($row[$i]);
if($make=='')continue;
$dtrow['Makes'][]=$make;
}
$data[] =$dtrow;
}
return $data;
}
您应该将 CSV 文件内容作为第一个参数传递。 对于第二个参数,如果您的 CSV 文件没有标头,则应传递 false
。编辑 ==>
您可以只修改此类中的几行以获得所需的结果。看看PTK ==>和<== PTK之间的那条线:
class CI_Csvreader {
var $fields; /** columns names retrieved after parsing */
var $separator = ','; /** separator used to explode each line */
/**
* Parse a text containing CSV formatted data.
*
* @access public
* @param string
* @return array
*/
function parse_text($p_Text) {
$lines = explode("n", $p_Text);
return $this->parse_lines($lines);
}
/**
* Parse a file containing CSV formatted data.
*
* @access public
* @param string
* @return array
*/
function parse_file($p_Filepath) {
$lines = file($p_Filepath);
return $this->parse_lines($lines);
}
/**
* Parse an array of text lines containing CSV formatted data.
*
* @access public
* @param array
* @return array
*/
function parse_lines($p_CSVLines) {
$content = FALSE;
foreach( $p_CSVLines as $line_num => $line ) {
if( $line != '' ) { // skip empty lines
$elements = explode($this->separator, $line);
if( !is_array($content) ) { // the first line contains fields names
$this->fields = $elements;
$content = array();
} else {
//PTK: ==> new code
$item=array('Years'=>$elements[0], 'Makes'=>array());
for($i=1;$i<count($elements);$i++)
{
$make=trim($elements[$i]);
//if($make=='')continue; //PTK: if you want to remove empty lines uncoment this line, but in this case your array may will have rows with different lengthes
$item['Makes'][]=$make;
}
//PTK: <== new code
/*
//PTK ==> original code
$item = array();
foreach( $this->fields as $id => $field ) {
if( isset($elements[$id]) ) {
$item[$field] = $elements[$id];
}
}
//PTK <== original code
*/
$content[] = $item;
}
}
}
return $content;
}