CSV 文件到 PHP 数组转换器
CSV file to PHP array converter
我有以下(部分)代码,它将使用数组检查表单中输入的值(如优惠券代码检查)。如果在表单中输入的值是数组中的值之一,人们可以在表单中输入代码。
if(!in_array($posted_value, array('DA001','DA002'))){ //
所以我有一个包含 80.000 个代码的 csv 文件。无论如何(在线转换器或其他东西)我可以将所有代码放在 ' ' 和 a 之间,所以现在的 csv 是:
DA001
DA002
DA003
IE302
我想把它转换成'DA001´, 'DA002', 'DA003', 'IE302'
--- 这是我的完整代码,包括您的代码:
我在与 .php 文件相同的目录中有 codes.csv
现在这是我的代码,但出现了问题,因为我有 500 服务器错误。
add_filter('frm_validate_field_entry', 'my_custom_validation', 10, 3);
function my_custom_validation($errors, $posted_field, $posted_value){
if($posted_field->id == 9){ //change 25 to the ID of the field to validate
$codes = file("codes.csv", FILE_IGNORE_NEW_LINES);
if (!in_array($posted_value, static $codes = array_flip(...);))){ //change 001 and 002 to your allowed values
//if it doesn't match up, add an error:
$errors['field'. $posted_field->id] = 'Deze code is al een keer gebruikt of bestaat niet.';
}
}
return $errors;
}
使用file()
函数将文件读入数组。每行将成为一个数组元素。
$codes = file("codes.csv", FILE_IGNORE_NEW_LINES);
if (!in_array($posted_value, $codes)) {
...
}
但是,搜索具有 80K 个元素的数组会很慢。如果您在同一个脚本中重复执行此操作,最好通过将其转换为关联数组来散列它们:
$codes = array_flip(file("codes.csv", FILE_IGNORE_NEW_LINES));
if (!isset($codes[$posted_value])) {
...
}
完整代码应该是:
add_filter('frm_validate_field_entry', 'my_custom_validation', 10, 3);
function my_custom_validation($errors, $posted_field, $posted_value){
if($posted_field->id == 9){ //change 25 to the ID of the field to validate
static $codes;
if (!$codes) {
$codes = array_flip(file("codes.csv", FILE_IGNORE_NEW_LINES));
}
if (!isset($codes[$posted_value])){ //change 001 and 002 to your allowed values
//if it doesn't match up, add an error:
$errors['field'. $posted_field->id] = 'Deze code is al een keer gebruikt of bestaat niet.';
}
}
return $errors;
}
我有以下(部分)代码,它将使用数组检查表单中输入的值(如优惠券代码检查)。如果在表单中输入的值是数组中的值之一,人们可以在表单中输入代码。
if(!in_array($posted_value, array('DA001','DA002'))){ //
所以我有一个包含 80.000 个代码的 csv 文件。无论如何(在线转换器或其他东西)我可以将所有代码放在 ' ' 和 a 之间,所以现在的 csv 是:
DA001
DA002
DA003
IE302
我想把它转换成'DA001´, 'DA002', 'DA003', 'IE302'
--- 这是我的完整代码,包括您的代码: 我在与 .php 文件相同的目录中有 codes.csv 现在这是我的代码,但出现了问题,因为我有 500 服务器错误。
add_filter('frm_validate_field_entry', 'my_custom_validation', 10, 3);
function my_custom_validation($errors, $posted_field, $posted_value){
if($posted_field->id == 9){ //change 25 to the ID of the field to validate
$codes = file("codes.csv", FILE_IGNORE_NEW_LINES);
if (!in_array($posted_value, static $codes = array_flip(...);))){ //change 001 and 002 to your allowed values
//if it doesn't match up, add an error:
$errors['field'. $posted_field->id] = 'Deze code is al een keer gebruikt of bestaat niet.';
}
}
return $errors;
}
使用file()
函数将文件读入数组。每行将成为一个数组元素。
$codes = file("codes.csv", FILE_IGNORE_NEW_LINES);
if (!in_array($posted_value, $codes)) {
...
}
但是,搜索具有 80K 个元素的数组会很慢。如果您在同一个脚本中重复执行此操作,最好通过将其转换为关联数组来散列它们:
$codes = array_flip(file("codes.csv", FILE_IGNORE_NEW_LINES));
if (!isset($codes[$posted_value])) {
...
}
完整代码应该是:
add_filter('frm_validate_field_entry', 'my_custom_validation', 10, 3);
function my_custom_validation($errors, $posted_field, $posted_value){
if($posted_field->id == 9){ //change 25 to the ID of the field to validate
static $codes;
if (!$codes) {
$codes = array_flip(file("codes.csv", FILE_IGNORE_NEW_LINES));
}
if (!isset($codes[$posted_value])){ //change 001 and 002 to your allowed values
//if it doesn't match up, add an error:
$errors['field'. $posted_field->id] = 'Deze code is al een keer gebruikt of bestaat niet.';
}
}
return $errors;
}