将 CSV 数字字符串转换为整数和浮点值?

Convert CSV number strings into integers and float values?

我正在导入 CSV,但不幸的是,csv 数字值作为字符串导入,我尝试通过转换 (int) & (float) 和使用 [= 将字符串转换为整数和浮点值16=] & floatval() 但我在结果和数据库中得到 00 值也存储为 00.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);
}

我的数组如下所示:

[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);
}

CSV 文件结构如下所示:

"Venue Name","","","Total Calls","Total %","Effective Calls","Effective %","Ineffective Calls","Ineffective %","Unique Calls","Unique %"
"Upstairs at Clarette","","","1","0.07","1","100.00","0","0.00","1","100.00"
"Blacks Members Club","","","1","0.07","1","100.00","0","0.00","1","100.00"
"Madison Roof Top Bar & Restaurant","","","8","0.54","8","100.00","0","0.00","8","100.00"
"Orphans Yard","","","0","0.00","0","","0","","0",""
"Oui 2 Rooms","","","7","0.47","7","100.00","0","0.00","5","71.43"
"Oval Space","","","7","0.47","4","57.14","3","42.86","6","85.71"
"OXO2","","","0","0.00","0","","0","","0",""
"Painters' Hall","","","0","0.00","0","","0","","0",""
"Dining Room at Clarette","","","0","0.00","0","","0","","0",""
"Park Lane Club London","","","0","0.00","0","","0","","0",""
"Park Village Studios","","","0","0.00","0","","0","","0",""
"Patron Bar a Vin","","","0","0.00","0","","0","","0",""
"PAUSE BAR","","","1","0.07","1","100.00","0","0.00","1","100.00"
"Peckham Springs","","","6","0.40","6","100.00","0","0.00","4","66.67"
"School of Wok","","","0","0.00","0","","0","","0",""
"Penton Street Gallery","","","1","0.07","1","100.00","0","0.00","1","100.00"
"Pewterers' Hall","","","3","0.20","1","33.33","2","66.67","1","33.33"
"Piccadilly Chambers","","","0","0.00","0","","0","","0",""
"Piccadilly Institute","","","1","0.07","1","100.00","0","0.00","1","100.00"
"Pop Brixton","","","5","0.34","5","100.00","0","0.00","4","80.00"
"Porchester Hall","","","1","0.07","1","100.00","0","0.00","1","100.00"
"Portland House","","","0","0.00","0","","0","","0",""
"Portobello Star","","","2","0.13","2","100.00","0","0.00","2","100.00"
"Prince of Peckham","","","1","0.07","1","100.00","0","0.00","1","100.00"
"Prince Philip House","","","0","0.00","0","","0","","0",""
"Private Dining Room at Albert's Club","","","0","0.00","0","","0","","0",""
"Proud Camden","","","0","0.00","0","","0","","0",""
"Proud Country House","","","3","0.20","2","66.67","1","33.33","2","66.67"
"Proud East","","","0","0.00","0","","0","","0",""
"Provender Warehouse","","","0","0.00","0","","0","","0",""
"Bocconcino","","","0","0.00","0","","0","","0",""
"Putney Studio","","","0","0.00","0","","0","","0",""
"Noho Showrooms","","","6","0.40","6","100.00","0","0.00","6","100.00"
"Quaglino's","","","0","0.00","0","","0","","0",""
"Radio Rooftop","","","1","0.07","1","100.00","0","0.00","1","100.00"
"Rail House Cafe","","","0","0.00","0","","0","","0",""
"Raven's Ait","","","0","0.00","0","","0","","0",""
"Refettorio Felix","","","3","0.20","3","100.00","0","0.00","2","66.67"
"Regent Street Cinema","","","7","0.47","4","57.14","3","42.86","4","57.14"
"Regent's Conferences & Events","","","1","0.07","0","0.00","1","100.00","1","100.00"
"RIBA Venues","","","0","0.00","0","","0","","0",""
"Rich Mix","","","2","0.13","2","100.00","0","0.00","2","100.00"
"Rida East","","","0","0.00","0","","0","","0",""
"River & Rowing Museum","","","4","0.27","4","100.00","0","0.00","4","100.00"
"Darbaar Restaurant","","","0","0.00","0","","0","","0",""
"Riverside Rooms","","","0","0.00","0","","0","","0",""
"Riverside Walkway","","","0","0.00","0","","0","","0",""
"Roast","","","0","0.00","0","","0","","0",""
"Royal London Suites","","","1","0.07","1","100.00","0","0.00","1","100.00"
"Colomba","","","3","0.20","1","33.33","2","66.67","3","100.00"
"Home House","","","0","0.00","0","","0","","0",""
"Rolling Stock","","","1","0.07","0","0.00","1","100.00","1","100.00"
"Many Hands Bermondsey","","","10","0.67","9","90.00","1","10.00","7","70.00"
"Roma","","","0","0.00","0","","0","","0",""
"Record Street Studios","","","0","0.00","0","","0","","0",""
"House 21","","","0","0.00","0","","0","","0",""
"Roundhouse","","","1","0.07","1","100.00","0","0.00","1","100.00"
"U Block","","","0","0.00","0","","0","","0",""
"Royal Horticultural Halls","","","6","0.40","6","100.00","0","0.00","6","100.00"
"Royal Institution of Great Britain","","","1","0.07","1","100.00","0","0.00","1","100.00"
"The Boiler House","","","0","0.00","0","","0","","0",""
"Rye London","","","0","0.00","0","","0","","0",""
"S&L Lounge","","","1","0.07","1","100.00","0","0.00","1","100.00"
"Saatchi Gallery","","","3","0.20","3","100.00","0","0.00","3","100.00"
"Sager & Wilde","","","0","0.00","0","","0","","0",""
"Sake no Hana","","","0","0.00","0","","0","","0",""
"Salut!","","","0","0.00","0","","0","","0",""
"Sans Pere Shoreditch","","","4","0.27","0","0.00","4","100.00","2","50.00"
"Searcys at 30 Pavilion Road","","","0","0.00","0","","0","","0",""
"Searcys Vintners' Hall","","","0","0.00","0","","0","","0",""
"Shaka Zulu","","","2","0.13","2","100.00","0","0.00","1","50.00"
"SHED","","","0","0.00","0","","0","","0",""
"Shoreditch Platform","","","4","0.27","4","100.00","0","0.00","3","75.00"
"Shoreditch Roundabout","","","0","0.00","0","","0","","0",""
"Shoreditch Studios","","","7","0.47","7","100.00","0","0.00","5","71.43"
"Shoreditch Town Hall","","","3","0.20","2","66.67","1","33.33","3","100.00"
"Shoreditch Treehouse","","","12","0.81","7","58.33","5","41.67","12","100.00"
"Simulacra Studio","","","0","0.00","0","","0","","0",""
"Six Storeys on Soho","","","0","0.00","0","","0","","0",""
"Sizona","","","9","0.61","8","88.89","1","11.11","6","66.67"
"Sky Garden","","","4","0.27","0","0.00","4","100.00","2","50.00"
"SMITHS' of Smithfield","","","1","0.07","1","100.00","0","0.00","1","100.00"
"Sorted","","","0","0.00","0","","0","","0",""
"South Place Hotel","","","6","0.40","5","83.33","1","16.67","2","33.33"
"Space 25","","","2","0.13","2","100.00","0","0.00","1","50.00"
"Speech Room","","","0","0.00","0","","0","","0",""
"Square Suite","","","1","0.07","1","100.00","0","0.00","1","100.00"

在尝试了很多不同的事情之后,经过了相当长一段时间的努力,最终简单的 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] => 
        )
)