拆分日期和时间然后放入其他列[关闭]
Posted
技术标签:
【中文标题】拆分日期和时间然后放入其他列[关闭]【英文标题】:Splitting date and time then put into other column [closed] 【发布时间】:2019-10-15 15:48:39 【问题描述】:我正在做一个小型约会时间项目。从我的 CSV 文件中,我有 4 列,称为名称、员工 ID、数据/时间、状态,在我的数据库中的列是名称、empID、日期、timeIn、timeOut、状态。
我的 csv 文件的图片:(图片 A)
这是我要保存在我的数据库中的内容:(图B)
示例输入 CSV 字符串:
Name,"Empoyee ID",Date/Time,Status
"Soriano, Jhoniel",901,"05/03/2019 1:01:03 PM",C/Out
"Soriano, Jhoniel",901,"05/04/2019 2:01:03 PM",C/In
"Soriano, Jhoniel",901,"05/04/2019 3:01:03 PM",C/Out
"Soriano, Jhoniel",901,"05/06/2019 4:01:03 PM",C/In
"Soriano, Jhoniel",901,"05/06/2019 5:01:03 PM",C/Out
"Soriano, Jhoniel",901,"05/07/2019 6:01:03 PM",C/In
我该如何着手解决这个问题?
编辑
我要感谢 @jimmix 给了我一些开始的想法。
这是真实的场景:
从我的 CSV 文件中,我有你可以在(图 A)找到的数据,然后我将使用我的 upload() 函数将表名“tbldumpbio”上传到我的 mysql 数据库中,
见下表结构:
从我的表 tbldumpbio 数据中,我有一个名为 processTimesheet() 的函数
代码如下:
public function processTimesheet()
$this->load->model('dbquery');
$query = $this->db->query("SELECT * FROM tbldumpbio");
foreach ($query->result() as $row)
$dateTimeExplArr = explode(' ', $row->datetimex);
$dateStr = $dateTimeExplArr[0];
$timeStr = $dateTimeExplArr[1];
if($row->status='C/Out' and !isset($timeStr) || empty($timeStr) )
$timeStrOut ='';
else
$timeStrOut = $dateTimeExplArr[1];
if($row->status='C/In' and !isset($timeStr) || empty($timeStr) )
$timeStrIn ='';
else
$timeStrIn = $dateTimeExplArr[1];
$data = array(
'ID' => '',
'companyAccessID' => '',
'name' => $row->name,
'empCompID' => $row->empid,
'date' => $dateStr,
'timeIn' => $timeStrIn,
'timeOut' => $timeStrOut,
'status' => '',
'inputType' => ''
);
$this->dbquery->modInsertval('tblempbioupload',$data);
此函数将添加另一个数据到 tblempbioupload。但这是我得到的结果:
请看以下数据:
问题是:
日期不应重复 如果状态为“C/In”,则应添加 Time In 数据 如果状态为“C/Out”,则应添加超时数据预期的结果应该是这样的:
【问题讨论】:
我猜有一些代码会生成这个 csv,除非你使用了黑魔法并且它会神奇地出现 请在您的 DarkMagic 课程中发布相关方法。 我们不在这里提供“肮脏的开头”。我们为展示研究和编码工作的问题提供清晰、专业、教育性的完整答案。现在,您的问题不清楚且过于宽泛,可能会吸引更多反对票。可能有数以千计的 *** 页面将为您提供“先机”。你在这里问了多个问题。 “如何解析 csv 文件” “如何分解日期” “如何有条件地将数据写入数组列” “如何查询数据库表” 感谢您提供的信息,但正如我所说,我只想问一些想法或逻辑,而不是整个代码。顺便说一句,谢谢。 @zeus2026 请发布下一次包含一些示例代码和/或输入和预期输出数据的问题,以便回答您的问题的人有一些工作要做,因为有很多人用有效的解决方案来回答,而不是仅仅给出如何解决问题的想法,但他们需要数据。要发布输入/输出数据,请使用 code 格式化按钮。 【参考方案1】:不是最短的,但可以工作:
<?php
$csvMultilineStr =
'Name,"Empoyee ID",Date/Time,Status
"Soriano, Jhoniel",901,"05/03/2019 1:01:03 PM",C/Out
"Soriano, Jhoniel",901,"05/04/2019 2:01:03 PM",C/In
"Soriano, Jhoniel",901,"05/04/2019 3:01:03 PM",C/Out
"Soriano, Jhoniel",901,"05/06/2019 4:01:03 PM",C/In
"Soriano, Jhoniel",901,"05/06/2019 5:01:03 PM",C/Out
"Soriano, Jhoniel",901,"05/07/2019 6:01:03 PM",C/In';
//read CSV as every line = 1 array item
$csvArr = explode("\n", $csvMultilineStr);
//get header line as array of column names
//and remove that line from $csvArr
$headerArr = str_getcsv(array_shift($csvArr));
$emplIdKeyStr = "Empoyee ID";
$resultArr = [];
/**
* Create array of arrays
* that each array inside an array
* is under key of emploeeId and
* represents one CSV row
*
* [
* 901 => [
* 0 => [
* 'Name' => value
* 'Empoyee ID' => value2
* ...
* ],
* 1 => [
* 'Name' => value
* 'Empoyee ID' => value2
* ...
* ]
* ]
* ]
*
*/
foreach($csvArr as $csvLineStr)
$csvEntryArr = str_getcsv($csvLineStr);
$csvLineArr = array_combine($headerArr, $csvEntryArr);
$emplIdInt = $csvLineArr[$emplIdKeyStr];
$resultArr[$emplIdInt][] = $csvLineArr;
var_export($resultArr);
/**
* Create array of arrays
* as above but with key => value
* structure
*
* [
* 901 => [
* '05/03/2019' => [
* 'C/In' => ...
* 'C/Out' => ...
* 'name' => ...
* ]
* ]
* ]
*
*/
$resultTimeIoArr = [];
$timeInOutColSwStr = 'Status';
$dateTimeColStr = 'Date/Time';
foreach($resultArr as $emplIdInt => $emplEntyArr)
foreach($emplEntyArr as $emplSingleEntryArr)
$dateTimeStr = $emplSingleEntryArr[$dateTimeColStr];
$dateTimeExplArr = explode(' ', $dateTimeStr);
$dateStr = $dateTimeExplArr[0];
$timeStr = $dateTimeExplArr[1];
$resultTimeIoArr[$emplIdInt][$dateStr][$emplSingleEntryArr[$timeInOutColSwStr]] = $timeStr;
$resultTimeIoArr[$emplIdInt][$dateStr]['name'] = '"' . $emplSingleEntryArr['Name'] . '"';
/**
* get sorted array by EmplId, Date, C/In or C/Out
* as an arry similar to first one CsvArr
*/
$resultCsvArr = [];
$resultCsvLineArr = [];
foreach($resultTimeIoArr as $emplIdInt => $singleEmplArr)
foreach($singleEmplArr as $dateStr => $signleIoArr)
$resultCsvLineArr['Name'] = $signleIoArr['name'];
$resultCsvLineArr['EmpID'] = $emplIdInt;
$resultCsvLineArr['Date'] = $dateStr;
if(!isset($signleIoArr['C/In']) || empty($signleIoArr['C/In']))
$timeIn = '';
else
$timeIn = $signleIoArr['C/In'];
if(!isset($signleIoArr['C/Out']) || empty($signleIoArr['C/Out']))
$timeOut = '';
else
$timeOut = $signleIoArr['C/Out'];
$resultCsvLineArr['timeIn'] = $timeIn;
$resultCsvLineArr['timeOut'] = $timeOut;
$resultCsvArr[] = $resultCsvLineArr;
echo "--- Array Result ---\n";
var_export($resultCsvArr);
//get header line as arry of column names
//from the keys of the first array
$head = array_keys($resultCsvArr[0]);
//put arr of header as 1st sting to csv string
$csvStr = implode(',',$head) . "\n";
//put all the rest in result sting
foreach($resultCsvArr as $resultEntryArr)
$csvStr .= implode(',',$resultEntryArr) . "\n";
// file_put_contents('path-to-file', $csvStr);
echo "\n\n --- CSV Result ---\n";
print_r($csvStr);
给出输出:
--- 数组结果---
array (
0 =>
array (
'Name' => '"Soriano, Jhoniel"',
'EmpID' => 901,
'Date' => '05/03/2019',
'timeIn' => '',
'timeOut' => '1:01:03',
),
1 =>
array (
'Name' => '"Soriano, Jhoniel"',
'EmpID' => 901,
'Date' => '05/04/2019',
'timeIn' => '2:01:03',
'timeOut' => '3:01:03',
),
2 =>
array (
'Name' => '"Soriano, Jhoniel"',
'EmpID' => 901,
'Date' => '05/06/2019',
'timeIn' => '4:01:03',
'timeOut' => '5:01:03',
),
3 =>
array (
'Name' => '"Soriano, Jhoniel"',
'EmpID' => 901,
'Date' => '05/07/2019',
'timeIn' => '6:01:03',
'timeOut' => '',
),
)
--- CSV 结果 ---
Name,EmpID,Date,timeIn,timeOut
"Soriano, Jhoniel",901,05/03/2019,,1:01:03
"Soriano, Jhoniel",901,05/04/2019,2:01:03,3:01:03
"Soriano, Jhoniel",901,05/06/2019,4:01:03,5:01:03
"Soriano, Jhoniel",901,05/07/2019,6:01:03,
使用:
file_put_contents('path-to-file', $csvStr);
最后将 csv 保存到文件中。
【讨论】:
谢谢@jimmix,我没想到有人会给我实际的代码,但我想要的只是获得一些想法或逻辑来开始。反正!再次感谢,这对我帮助很大。 :D 上帝保佑 @zeus2026 欢迎您。用 cmets 查看我更新的代码以了解这个想法。如果您同意,也可以考虑accepting answer。 嗨@jimmix,谢谢,但我已经编辑了我的问题,请查看我的编辑问题。抱歉,如果我没有澄清我的问题。以上是关于拆分日期和时间然后放入其他列[关闭]的主要内容,如果未能解决你的问题,请参考以下文章