本文整理汇总了PHP中CSV::add_row方法的典型用法代码示例。如果您正苦于以下问题:PHP CSV::add_row方法的具体用法?PHP CSV::add_row怎么用?PHP CSV::add_row使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CSV
的用法示例。
在下文中一共展示了CSV::add_row方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: load
/**
* This function will load a CSV file.
*
* @access public
* @static
* @param array $config the configuration array
* @return CSV an instance of the CSV class containing
* the contents of the file.
*
* @see http://www.php.net/manual/en/function.fgetcsv.php
*/
public static function load($config = array())
{
$csv = new CSV($config);
if (file_exists($csv->file_name)) {
if (($fp = fopen($csv->file_name, 'r')) !== FALSE) {
$eol = $csv->eol == "\r\n" ? array(13, 10) : array(ord($csv->eol));
// 13 => cr, 10 => lf
$buffer = '';
while (($char = fgetc($fp)) !== FALSE) {
// load char by char, to replace line endings
if (in_array(ord($char), $eol)) {
$buffer .= "\r\n";
} else {
$buffer .= $char;
}
}
fclose($fp);
$rows = explode("\r\n", $buffer);
$enclosure = $csv->enclosure;
$delimiter = $enclosure . $csv->delimiter . $enclosure;
if (empty($enclosure)) {
$enclosure = " \t\n\r\v";
}
$regex = '/' . $delimiter . '/';
foreach ($rows as $row) {
$row = trim($row, $enclosure);
//$columns = explode($delimiter, $row);
$columns = preg_split($regex, $row);
$csv->add_row($columns);
}
}
}
return $csv;
}
示例2: as_csv
/**
* This function will create an instance of the CSV class using the data contained
* in the result set.
*
* @access public
* @param array $config the configuration array
* @return CSV an instance of the CSV class
*/
public function as_csv(array $config = array())
{
$csv = new CSV($config);
if ($this->is_loaded()) {
switch ($this->type) {
case 'array':
case 'object':
foreach ($this->records as $record) {
$csv->add_row((array) $record);
}
break;
default:
if (class_exists($this->type)) {
if ($this->records[0] instanceof DB_ORM_Model or method_exists($this->records[0], 'as_array')) {
foreach ($this->records as $record) {
$csv->add_row($record->as_array());
}
} else {
if ($this->records[0] instanceof Iterator) {
foreach ($this->records as $record) {
$row = array();
foreach ($record as $column) {
$row[] = $column;
}
$csv->add_row($row);
}
} else {
foreach ($this->records as $record) {
$csv->add_row(get_object_vars($record));
}
}
}
}
break;
}
}
return $csv;
}