PHP 重命名数组键时遇到问题
我正在读取数百个 CSV 文件并将其导入数据库。
某些列名称包含相同的数据,但在所有文件中具有不同的名称。
例如,一个文件将显示“电话”,另一个文件将显示“晚间电话”...两者都包含相同的数据,但名称不同。
我试图将所有内容重命名为 EveningPhone,因为当我意识到不同文件中有不同的标头名称时,我已经完成了大部分代码。太懒了,无法替换我猜的所有内容。
我正在创建一个关联数组来匹配数据,然后转到它所在的数据库。
问题是我无法获取重命名为一种约定的键。这是发生这种情况的函数:
public function readCSV($file) {
/**
* @todo: LOAD THE FILE INTO A MULTIDIMENSIONAL ASSOCIATIVE ARRAY TO DETERMINE THE FILEDS
*/
// Read the first line to get the headers
$headers = fgetcsv($file);
if (!array_key_exists('EveningPhone', $headers)) {
if (array_key_exists('Phone', $headers)) {
$headers['EveningPhone'] = $headers['Phone'];
unset($headers['Phone']);
} else {
die("other");
}
}
format::neat_r($headers); // basically print_r but adds a new line to read it easier...
die();
数组在之前和之后返回,如下所示:
LeadID
AddDate 姓 名 地址 城市 状态 邮政编码 潜在客户定位器 电话 电子邮件 性别 评论
最后,我仍然从使用 Phone 作为电话号码的文件中得到 EveningPhone 未定义错误。
有人有什么想法吗?
I am reading hundreds of CSVs to import into a DB.
Some of the column names contain the same data but have different names across all the files.
For example, one file will say Phone where the other says EveningPhone...both contain the same data, different names.
I am trying to rename everything to EveningPhone because I had most of the code done when i realized there were different header names in different files. Too lazy to replace all i guess..
I am creating an assoc array to match the data then off to the db it goes..
The problem is I cannot get the keys to rename to one convention. Here is the function where it happens:
public function readCSV($file) {
/**
* @todo: LOAD THE FILE INTO A MULTIDIMENSIONAL ASSOCIATIVE ARRAY TO DETERMINE THE FILEDS
*/
// Read the first line to get the headers
$headers = fgetcsv($file);
if (!array_key_exists('EveningPhone', $headers)) {
if (array_key_exists('Phone', $headers)) {
$headers['EveningPhone'] = $headers['Phone'];
unset($headers['Phone']);
} else {
die("other");
}
}
format::neat_r($headers); // basically print_r but adds a new line to read it easier...
die();
The array is returned before and after as this:
LeadID
AddDate
LastName
FirstName
Address
City
State
ZipCode
LeadLocator
Phone
Email
Sex
Comments
At the end, I still get EveningPhone not defined errors from a file that uses Phone as the phone number..
Anybody have any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用,
因为在您通过
fgetcsv
读取后,$headers
数组是一个普通的索引列表。因此 array_key_exists 将不起作用。您稍后可能会将其用作字典,但此时它还不是数组键。要替换条目,请使用:
You need to use
because the
$headers
array is a normal indexed list after you just read it viafgetcsv
. Therefore array_key_exists won't work. You might later use it as dict, but at this point it isn't yet an array key.To replace the entry use:
fgetcsv 不返回关联数组。它将返回一个包含数据作为值的数组,因此您应该使用 in_array 或 array_search (当您需要密钥时)。读取实际数据的代码可能使用 array_combine 使用标头构建关联数组。
fgetcsv does not return an associative array. It will return an array with the data as values so you should be using in_array or array_search (when you need the key). The code that reads the actual data is probably using array_combine to build an associative array using the header.