我正在导入CSV,但是不幸的是,csv数字值已作为字符串导入,我尝试通过强制转换(int)
和(float)
并使用{{1}将字符串转换为整数和浮点值}和intval()
,但结果为floatval()
和0
,在数据库中,值也分别存储为0
和0
,因为必须严格添加字段只接受整数和浮点值,不接受字符串。
那么我该如何解决?
我也使用了一个函数对其进行编码,但结果仍然相同:
0.00
我的数组如下:
/**
* Convert csv source data into correct encoding data
*
*/
protected function encodeField($field) {
return iconv(mb_detect_encoding($field, mb_detect_order(), true), "iso-8859-1", $field);
}
我的CSV导入代码:
[02-Aug-2018 12:22:36 Europe/Berlin] Array
(
[1] => Array
(
[name] => Upstairs at Clarette
[month] => 7
[year] => 2018
[status] =>
[total_calls] => "1"
[total_call_per] => "0.07"
[effective_calls] => "1"
[effective_per] => "100.00"
[ineffective_calls] => "0"
[ineffective_per] => "0.00"
[unique_calls] => "1"
[unique_per] => "100.00"
)
[2] => Array
(
[name] => Blacks Members Club
[month] => 7
[year] => 2018
[status] =>
[total_calls] => "1"
[total_call_per] => "0.07"
[effective_calls] => "1"
[effective_per] => "100.00"
[ineffective_calls] => "0"
[ineffective_per] => "0.00"
[unique_calls] => "1"
[unique_per] => "100.00"
)
[3] => Array
(
[name] => Madison Roof Top Bar & Restaurant
[month] => 7
[year] => 2018
[status] =>
[total_calls] => "8"
[total_call_per] => "0.54"
[effective_calls] => "8"
[effective_per] => "100.00"
[ineffective_calls] => "0"
[ineffective_per] => "0.00"
[unique_calls] => "8"
[unique_per] => "100.00"
)
[4] => Array
(
[name] => Orphans Yard
[month] => 7
[year] => 2018
[status] =>
[total_calls] => "0"
[total_call_per] => "0.00"
[effective_calls] => "0"
[effective_per] => ""
[ineffective_calls] => "0"
[ineffective_per] => ""
[unique_calls] => "0"
[unique_per] => ""
)
[5] => Array
(
[name] => Oui 2 Rooms
[month] => 7
[year] => 2018
[status] =>
[total_calls] => "7"
[total_call_per] => "0.47"
[effective_calls] => "7"
[effective_per] => "100.00"
[ineffective_calls] => "0"
[ineffective_per] => "0.00"
[unique_calls] => "5"
[unique_per] => "71.43"
)
[6] => Array
(
[name] => Oval Space
[month] => 7
[year] => 2018
[status] =>
[total_calls] => "7"
[total_call_per] => "0.47"
[effective_calls] => "4"
[effective_per] => "57.14"
[ineffective_calls] => "3"
[ineffective_per] => "42.86"
[unique_calls] => "6"
[unique_per] => "85.71"
)
[7] => Array
(
[name] => OXO2
[month] => 7
[year] => 2018
[status] =>
[total_calls] => "0"
[total_call_per] => "0.00"
[effective_calls] => "0"
[effective_per] => ""
[ineffective_calls] => "0"
[ineffective_per] => ""
[unique_calls] => "0"
[unique_per] => ""
)
[8] => Array
(
[name] => Painters' Hall
[month] => 7
[year] => 2018
[status] =>
[total_calls] => "0"
[total_call_per] => "0.00"
[effective_calls] => "0"
[effective_per] => ""
[ineffective_calls] => "0"
[ineffective_per] => ""
[unique_calls] => "0"
[unique_per] => ""
)
[9] => Array
(
[name] => Dining Room at Clarette
[month] => 7
[year] => 2018
[status] =>
[total_calls] => "0"
[total_call_per] => "0.00"
[effective_calls] => "0"
[effective_per] => ""
[ineffective_calls] => "0"
[ineffective_per] => ""
[unique_calls] => "0"
[unique_per] => ""
)
[10] => Array
(
[name] => Park Lane Club London
[month] => 7
[year] => 2018
[status] =>
[total_calls] => "0"
[total_call_per] => "0.00"
[effective_calls] => "0"
[effective_per] => ""
[ineffective_calls] => "0"
[ineffective_per] => ""
[unique_calls] => "0"
[unique_per] => ""
)
[11] => Array
(
[name] => Park Village Studios
[month] => 7
[year] => 2018
[status] =>
[total_calls] => "0"
[total_call_per] => "0.00"
[effective_calls] => "0"
[effective_per] => ""
[ineffective_calls] => "0"
[ineffective_per] => ""
[unique_calls] => "0"
[unique_per] => ""
)
[12] => Array
(
[name] => Patron Bar a Vin
[month] => 7
[year] => 2018
[status] =>
[total_calls] => "0"
[total_call_per] => "0.00"
[effective_calls] => "0"
[effective_per] => ""
[ineffective_calls] => "0"
[ineffective_per] => ""
[unique_calls] => "0"
[unique_per] => ""
)
[13] => Array
(
[name] => PAUSE BAR
[month] => 7
[year] => 2018
[status] =>
[total_calls] => "1"
[total_call_per] => "0.07"
[effective_calls] => "1"
[effective_per] => "100.00"
[ineffective_calls] => "0"
[ineffective_per] => "0.00"
[unique_calls] => "1"
[unique_per] => "100.00"
)
[14] => Array
(
[name] => Peckham Springs
[month] => 7
[year] => 2018
[status] =>
[total_calls] => "6"
[total_call_per] => "0.40"
[effective_calls] => "6"
[effective_per] => "100.00"
[ineffective_calls] => "0"
[ineffective_per] => "0.00"
[unique_calls] => "4"
[unique_per] => "66.67"
)
[15] => Array
(
[name] => School of Wok
[month] => 7
[year] => 2018
[status] =>
[total_calls] => "0"
[total_call_per] => "0.00"
[effective_calls] => "0"
[effective_per] => ""
[ineffective_calls] => "0"
[ineffective_per] => ""
[unique_calls] => "0"
[unique_per] => ""
)
[16] => Array
(
[name] => Penton Street Gallery
[month] => 7
[year] => 2018
[status] =>
[total_calls] => "1"
[total_call_per] => "0.07"
[effective_calls] => "1"
[effective_per] => "100.00"
[ineffective_calls] => "0"
[ineffective_per] => "0.00"
[unique_calls] => "1"
[unique_per] => "100.00"
)
[17] => Array
(
[name] => Pewterers' Hall
[month] => 7
[year] => 2018
[status] =>
[total_calls] => "3"
[total_call_per] => "0.20"
[effective_calls] => "1"
[effective_per] => "33.33"
[ineffective_calls] => "2"
[ineffective_per] => "66.67"
[unique_calls] => "1"
[unique_per] => "33.33"
)
[18] => Array
(
[name] => Piccadilly Chambers
[month] => 7
[year] => 2018
[status] =>
[total_calls] => "0"
[total_call_per] => "0.00"
[effective_calls] => "0"
[effective_per] => ""
[ineffective_calls] => "0"
[ineffective_per] => ""
[unique_calls] => "0"
[unique_per] => ""
)
[19] => Array
(
[name] => Piccadilly Institute
[month] => 7
[year] => 2018
[status] =>
[total_calls] => "1"
[total_call_per] => "0.07"
[effective_calls] => "1"
[effective_per] => "100.00"
[ineffective_calls] => "0"
[ineffective_per] => "0.00"
[unique_calls] => "1"
[unique_per] => "100.00"
)
)
CSV文件结构如下:
/**
* import mediahawk csv and store data
*
* @param sfWebRequest $request
*/
public function executeImportMediahawkCsv(sfWebRequest $request) {
$this->form = new importMediahawkStatForm();
$this->form->setDefault('month', date('m'));
$this->form->setDefault('year', date('Y'));
$this->exceptions = $this->notRelatedRecords = array();
if ($request->isMethod('POST')) {
$params = $request->getParameter($this->form->getName());
$files = $request->getFiles($this->form->getName());
$this->form->bind($params, $files);
if ($this->form->isValid()) {
$month = $params['month'];
$year = $params['year'];
$oldData = Doctrine::getTable('MediahawkStat')->findByMonthAndYear($month, $year);
$file = $files['file']['tmp_name'];
$handle = fopen($file, "r");
$csvRecords = array();
$venueNames = array(); // store venue names
$i = 0;
while ($data = fgetcsv($handle, 1000, ",", "'")) {
if ($i > 0 && trim($data['0']) != 'Totals') {
$name = str_replace('"', '', $data['0']);
$venueNames[] = $this->encodeField($name);
$csvRecords[$i]['name'] = $name;
$csvRecords[$i]['month'] = $month;
$csvRecords[$i]['year'] = $year;
$csvRecords[$i]['status'] = '';
$csvRecords[$i]['total_calls'] = $data['3'];
$csvRecords[$i]['total_call_per'] = $data['4'];
$csvRecords[$i]['effective_calls'] = $data['5'];
$csvRecords[$i]['effective_per'] = $data['6'];
$csvRecords[$i]['ineffective_calls'] = $data['7'];
$csvRecords[$i]['ineffective_per'] = $data['8'];
$csvRecords[$i]['unique_calls'] = $data['9'];
$csvRecords[$i]['unique_per'] = $data['10'];
}
$i++;
}
error_log(print_r($csvRecords, true));
// Store csv data in table
if (count($csvRecords) > 0) {
// Fetch venue ids by venue names
$venueIds = Doctrine::getTable('Location')->getIdsByNames($venueNames);
foreach ($csvRecords as $csvRecord) {
$venueName = strtolower($csvRecord['name']);
$venueId = isset($venueIds[$venueName]) ? $venueIds[$venueName] : '';
try {
if (isset($oldData[$venueId])) {
$stat = $oldData[$venueId];
} else {
$stat = new MediahawkStat();
}
if ($venueId)
$stat->setLocationId($venueId);
$stat->setCampaign($csvRecord['name']);
$stat->setMonth($csvRecord['month']);
$stat->setYear($csvRecord['year']);
$stat->setStatus($csvRecord['status']);
$stat->setTotalCalls($csvRecord['total_calls']);
$stat->setTotalPercent($csvRecord['total_call_per']);
$stat->setEffectiveCalls($csvRecord['effective_calls']);
$stat->setEffectivePercent($csvRecord['effective_per']);
$stat->setIneffectiveCalls($csvRecord['ineffective_calls']);
$stat->setIneffectivePercent($csvRecord['ineffective_per']);
$stat->setUniqueCalls($csvRecord['unique_calls']);
$stat->setUniquePercent($csvRecord['unique_per']);
$stat->save();
if (!$venueId) {
$csvRecord['id'] = $stat->getId();
$this->notRelatedRecords[] = $csvRecord;
}
} catch (Exception $e) {
$this->exceptions[] = $csvRecord;
}
}
if (count($this->exceptions) == 0 && count($this->notRelatedRecords) == 0) {
echo '<script type="text/javascript">
window.parent.$.fancybox.close();
alert("Data imported successfully");
window.parent.mediahawk_stat_load_table();
</script>';
} else {
echo '<script type="text/javascript">
alert("Data imported successfully");
window.parent.mediahawk_stat_load_table();
</script>';
}
}
}
}
$this->setLayout(false);
}
答案 0 :(得分:1)
在苦苦挣扎了一段时间之后,终于尝试了不同的事情,简单的str_replace()
函数帮助并编写了以下简单函数来完成这项工作:
/**
* Remove quotes from the numerical values
*/
protected function removeQuotes($field) {
return str_replace('"', '', $field);
}
将上述功能应用于所有字段后,现在一切都可以正常运行:
更新的代码部分:
$name = $this->removeQuotes($data['0']);
$venueNames[] = $this->encodeField($name);
$csvRecords[$i]['name'] = $name;
$csvRecords[$i]['month'] = $month;
$csvRecords[$i]['year'] = $year;
$csvRecords[$i]['status'] = '';
$csvRecords[$i]['total_calls'] = $this->removeQuotes($data['3']);
$csvRecords[$i]['total_call_per'] = $this->removeQuotes($data['4']);
$csvRecords[$i]['effective_calls'] = $this->removeQuotes($data['5']);
$csvRecords[$i]['effective_per'] = $this->removeQuotes($data['6']);
$csvRecords[$i]['ineffective_calls'] = $this->removeQuotes($data['7']);
$csvRecords[$i]['ineffective_per'] = $this->removeQuotes($data['8']);
$csvRecords[$i]['unique_calls'] = $this->removeQuotes($data['9']);
$csvRecords[$i]['unique_per'] = $this->removeQuotes($data['10']);
现在正确的数组输出:
[02-Aug-2018 13:52:36 Europe/Berlin] Array
(
[1] => Array
(
[name] => Upstairs at Clarette
[month] => 7
[year] => 2018
[status] =>
[total_calls] => 1
[total_call_per] => 0.07
[effective_calls] => 1
[effective_per] => 100.00
[ineffective_calls] => 0
[ineffective_per] => 0.00
[unique_calls] => 1
[unique_per] => 100.00
)
[2] => Array
(
[name] => Blacks Members Club
[month] => 7
[year] => 2018
[status] =>
[total_calls] => 1
[total_call_per] => 0.07
[effective_calls] => 1
[effective_per] => 100.00
[ineffective_calls] => 0
[ineffective_per] => 0.00
[unique_calls] => 1
[unique_per] => 100.00
)
[3] => Array
(
[name] => Madison Roof Top Bar & Restaurant
[month] => 7
[year] => 2018
[status] =>
[total_calls] => 8
[total_call_per] => 0.54
[effective_calls] => 8
[effective_per] => 100.00
[ineffective_calls] => 0
[ineffective_per] => 0.00
[unique_calls] => 8
[unique_per] => 100.00
)
[4] => Array
(
[name] => Orphans Yard
[month] => 7
[year] => 2018
[status] =>
[total_calls] => 0
[total_call_per] => 0.00
[effective_calls] => 0
[effective_per] =>
[ineffective_calls] => 0
[ineffective_per] =>
[unique_calls] => 0
[unique_per] =>
)
[5] => Array
(
[name] => Oui 2 Rooms
[month] => 7
[year] => 2018
[status] =>
[total_calls] => 7
[total_call_per] => 0.47
[effective_calls] => 7
[effective_per] => 100.00
[ineffective_calls] => 0
[ineffective_per] => 0.00
[unique_calls] => 5
[unique_per] => 71.43
)
[6] => Array
(
[name] => Oval Space
[month] => 7
[year] => 2018
[status] =>
[total_calls] => 7
[total_call_per] => 0.47
[effective_calls] => 4
[effective_per] => 57.14
[ineffective_calls] => 3
[ineffective_per] => 42.86
[unique_calls] => 6
[unique_per] => 85.71
)
[7] => Array
(
[name] => OXO2
[month] => 7
[year] => 2018
[status] =>
[total_calls] => 0
[total_call_per] => 0.00
[effective_calls] => 0
[effective_per] =>
[ineffective_calls] => 0
[ineffective_per] =>
[unique_calls] => 0
[unique_per] =>
)
[8] => Array
(
[name] => Painters' Hall
[month] => 7
[year] => 2018
[status] =>
[total_calls] => 0
[total_call_per] => 0.00
[effective_calls] => 0
[effective_per] =>
[ineffective_calls] => 0
[ineffective_per] =>
[unique_calls] => 0
[unique_per] =>
)
[9] => Array
(
[name] => Dining Room at Clarette
[month] => 7
[year] => 2018
[status] =>
[total_calls] => 0
[total_call_per] => 0.00
[effective_calls] => 0
[effective_per] =>
[ineffective_calls] => 0
[ineffective_per] =>
[unique_calls] => 0
[unique_per] =>
)
[10] => Array
(
[name] => Park Lane Club London
[month] => 7
[year] => 2018
[status] =>
[total_calls] => 0
[total_call_per] => 0.00
[effective_calls] => 0
[effective_per] =>
[ineffective_calls] => 0
[ineffective_per] =>
[unique_calls] => 0
[unique_per] =>
)
)