php array_intersect 没有得到第一个值
php array_intersect don't get the first value
基本上,我正在尝试将 csv 数据导入我的数据库。在将数据插入数据库之前,我想检查数据库是否有该列(来自 csv headers)。
// read csv from file ex: data.csv
$reader = \League\Csv\Reader::createFromPath($file);
// get first row from csv as headers (email, first_name, last_name etc.)
$headers = array_filter(array_map(function($value) { return strtolower(trim($value)); }, $reader->fetchOne()));
// get fields from database (email, first_name, last_name, birthdate etc.)
$fields = collect($this->fields)->map(function ($field) {
return strtolower($field->tag);
})->toArray();
// want to check which fields has csv
$availableFields = array_intersect($headers, $fields);
但是当我运行上面的代码时,array_intersect无法正常工作。
输出为:
$headers : Array ([0] => email [1] => first_name [2] => last_name )
$fields : Array ( [0] => email [1] => first_name [2] => last_name [3] => telephone [4] => gender [5] => birthdate [6] => country )
$availableFields :Array ([1] => first_name [2] => last_name )
var_dump 结果是:
$headers : array(3) {
[0]=>
string(8) "email"
[1]=>
string(10) "first_name"
[2]=>
string(9) "last_name"
}
$fields : array(9) {
[0]=>
string(5) "email"
[1]=>
string(10) "first_name"
[2]=>
string(9) "last_name"
[3]=>
string(9) "telephone"
[4]=>
string(6) "gender"
[5]=>
string(9) "birthdate"
[6]=>
string(7) "country"
}
trim()
function in php cuts off only 6 non-printable characters per default. I think that's your problem because there are more non-printable characters (list of characters).
例如:
<?php
var_dump([trim("EMAIL\x1C")]);
trim() 允许您在第二个参数中指定字符掩码:
<?php
trim($binary, "\x00..\x1F");
而不是使用 trim(),正则表达式提供了更多的保证:
<?php
// preg_replace('/[\x00-\x1F\x7F]/u', '', $value)
$headers = array_filter(array_map(function($value) { return strtolower(preg_replace('/[\x00-\x1F\x7F]/u', '', $value)); }, $reader->fetchOne()));
基本上,我正在尝试将 csv 数据导入我的数据库。在将数据插入数据库之前,我想检查数据库是否有该列(来自 csv headers)。
// read csv from file ex: data.csv
$reader = \League\Csv\Reader::createFromPath($file);
// get first row from csv as headers (email, first_name, last_name etc.)
$headers = array_filter(array_map(function($value) { return strtolower(trim($value)); }, $reader->fetchOne()));
// get fields from database (email, first_name, last_name, birthdate etc.)
$fields = collect($this->fields)->map(function ($field) {
return strtolower($field->tag);
})->toArray();
// want to check which fields has csv
$availableFields = array_intersect($headers, $fields);
但是当我运行上面的代码时,array_intersect无法正常工作。
输出为:
$headers : Array ([0] => email [1] => first_name [2] => last_name )
$fields : Array ( [0] => email [1] => first_name [2] => last_name [3] => telephone [4] => gender [5] => birthdate [6] => country )
$availableFields :Array ([1] => first_name [2] => last_name )
var_dump 结果是:
$headers : array(3) {
[0]=>
string(8) "email"
[1]=>
string(10) "first_name"
[2]=>
string(9) "last_name"
}
$fields : array(9) {
[0]=>
string(5) "email"
[1]=>
string(10) "first_name"
[2]=>
string(9) "last_name"
[3]=>
string(9) "telephone"
[4]=>
string(6) "gender"
[5]=>
string(9) "birthdate"
[6]=>
string(7) "country"
}
trim()
function in php cuts off only 6 non-printable characters per default. I think that's your problem because there are more non-printable characters (list of characters).
例如:
<?php
var_dump([trim("EMAIL\x1C")]);
trim() 允许您在第二个参数中指定字符掩码:
<?php
trim($binary, "\x00..\x1F");
而不是使用 trim(),正则表达式提供了更多的保证:
<?php
// preg_replace('/[\x00-\x1F\x7F]/u', '', $value)
$headers = array_filter(array_map(function($value) { return strtolower(preg_replace('/[\x00-\x1F\x7F]/u', '', $value)); }, $reader->fetchOne()));