我正在使用Excel文件中的文件导入功能。 导入期间,我正在使用PhpSpreadSheet库将该文件转换为CSV格式。 接下来,我的函数extractData()将循环csv文件,检测特定的标头,组合与之对应的数据,然后将其插入数据库。
我在这些计算机上的配置:
Dev机器:
我的算法在我的本地计算机上运行良好,但是当我在开发计算机上对其进行测试时,它会将这些错误发回给我:
PHP警告:array_combine()期望参数2为数组,为null 给
PHP警告:array_intersect_key():预期参数1为 数组,给定null
PHP在Debian 8上是否更稳定,在Debian 9上还不够?
如何解决此问题?
CSV示例:
const PATTERNS = [
'fullName' => '/^(?:noms?\/pré?e?noms?)/ui',
'lastName' => '/^(?:votre nom|nom)/ui',
'firstName' => '/^(?:votre prénom|prénom|prénoms)/ui',
'zipcode' => '/^(?:secteur|commune|zone|ville hbt|Dans quelle zone géographique habitez-vous \?)/ui',
'birthday' => '/^(?:votre date de naissance|Date de naissance|En quelle année êtes-vous née?)/ui',
'gender' => '/^(?:votre sexe|sexe)/ui',
'contact' => '/^(?:contact tel\/mail|contacts?)/ui',
'postalAddress' => '/^(?:adresse|zone hbt|secteur d\'habitation)/ui',
'phoneNumber' => '/^(?:votre numéro de téléphone|Téléphone|n° de téléphone|contact tel|tel)/ui',
'employment' => "/^(?:votre profession|votre métier|profession|métier|dans quel(?:\(s\))? secteur(?:\(s\))? d'activité exercez-vous cette activité \?|secteur d'activité)/ui",
'email' => '/^(?:votre adresse e-mail|votre adresse mail|votre mail|votre email|e-mail|mail)/ui',
'ratings' => '/^(?:appréciation)/ui',
'comments' => '/^(?:commentaires?|remarque)/ui',
];
function extractData($file, $cnx)
{
$headers = [];
$state = 0;
$flag = true;
if(($handle = fopen($file, 'r')) !== FALSE)
{
foreach (fgetcsv($handle, 1000, ";") as $key => $header)
{
foreach (PATTERNS as $symbol => $pattern)
{
if (!in_array($symbol, $headers) && preg_match($pattern, $header))
{
$headers[$key] = $symbol;
break;
}
}
}
// Loop file from lign 2 to more
while ($line = fgetcsv($handle, 1000, ";"))
{
$line = array_combine($headers, array_intersect_key($line, $headers));
if($line['lastName'] === "" || $line['firstName'] === "")
{
continue;
}
if($line['fullName'] !== NULL)
{
$formatNames = formatNames($line['fullName']);
$line['lastName'] = $formatNames[0];
$line['firstName'] = $formatNames[1];
}
if($line['contact'] !== NULL)
{
$formatContact = formatContact($line['contact']);
$line['phoneNumber'] = $formatContact[0];
$line['email'] = $formatContact[1];
}
$lastName = $line['lastName'] !== NULL ? $line['lastName'] : "";
$firstName = $line['firstName'] !== NULL ? $line['firstName'] : "";
$formatZipcode = translateZipcode($line['zipcode']);
$zipcode = $formatZipcode !== NULL ? $formatZipcode : "";
$birthday = $line['birthday'] !== NULL ? $line['birthday'] : "";
$gender = $line['gender'] !== NULL ? $line['gender'] : "";
$postalAddress = $line['postalAddress'] !== NULL ? $line['postalAddress'] : "";
$formatPhoneNumber = formatPhoneNumber($line['phoneNumber']);
if($line['contact'] !== NULL)
{
$phoneNumber = $line['phoneNumber'] !== NULL ? $line['phoneNumber'] : "";
}
else
{
$phoneNumber = $formatPhoneNumber !== NULL ? $formatPhoneNumber : "";
}
$availableDays = $line['availableDays'] !== NULL ? $line['availableDays'] : "";
$employment = $line['employment'] !== NULL ? $line['employment'] : "";
$transportMeans = $line['transportMeans'] !== NULL ? $line['transportMeans'] : "";
$candidacyType = $line['candidacyType'] !== NULL ? $line['candidacyType'] : "";
$email = $line['email'] !== NULL ? $line['email'] : "";
$query = $cnx->prepare('INSERT INTO candidacies (lastName, firstName, zipcode, birthday, gender, postalAddress, phoneNumber, availableDays, employment, transportMeans, candidacyType, email, candidacies_UserId, candidacy_CreatedAt, state) VALUES (:lastName, :firstName, :zipcode, :birthday, :gender, :postalAddress, :phoneNumber, :availableDays, :employment, :transportMeans, :candidacyType, :email, :candidacies_UserId, NOW(), :state)');
$query->bindValue(':lastName', $lastName, PDO::PARAM_STR);
$query->bindValue(':firstName', $firstName, PDO::PARAM_STR);
$query->bindValue(':zipcode', $zipcode, PDO::PARAM_STR);
$query->bindValue(':birthday', $birthday, PDO::PARAM_STR);
$query->bindValue(':gender', $gender, PDO::PARAM_STR);
$query->bindValue(':postalAddress', $postalAddress, PDO::PARAM_STR);
$query->bindValue(':phoneNumber', $phoneNumber, PDO::PARAM_STR);
$query->bindValue(':availableDays', $availableDays, PDO::PARAM_STR);
$query->bindValue(':employment', $employment, PDO::PARAM_STR);
$query->bindValue(':transportMeans', $transportMeans, PDO::PARAM_STR);
$query->bindValue(':candidacyType', $candidacyType, PDO::PARAM_STR);
$query->bindValue(':email', $email, PDO::PARAM_STR);
$query->bindValue(':candidacies_UserId', $_SESSION['user']->userId, PDO::PARAM_INT);
$query->bindValue(':state', "In progress", PDO::PARAM_STR);
if($query->execute())
{
$state = 1;
}
$query = $cnx->prepare('SELECT MAX(candidacyId) AS id FROM candidacies');
$query->execute();
$getCandidacyId = $query->fetch(PDO::FETCH_OBJ);
if($line['comments'] !== NULL)
{
$comments = $line['comments'];
if($comments === "")
{
continue;
}
$query = $cnx->prepare('INSERT INTO comments (comments, comment_CreatedAt, comments_UserId, comments_CandidaciesId) VALUES (:comments, NOW(), :comments_UserId, :comments_CandidaciesId)');
$query->bindValue(':comments', $comments, PDO::PARAM_STR);
$query->bindValue(':comments_UserId', $_SESSION['user']->userId, PDO::PARAM_INT);
$query->bindValue(':comments_CandidaciesId', $getCandidacyId->id, PDO::PARAM_INT);
if($query->execute())
{
$state = 1;
}
}
$query = $cnx->prepare('SELECT MAX(commentsId) AS id FROM comments');
$query->execute();
$getCommentId = $query->fetch(PDO::FETCH_OBJ);
if($line['ratings'] !== NULL)
{
$ratings = formatRatings($line['ratings']);
if($ratings === "")
{
continue;
}
$query = $cnx->prepare('INSERT INTO ratings (ratings, rating_CreatedAt, ratings_UserId, ratings_CandidacyId, ratings_CommentId) VALUES (:ratings, NOW(), :ratings_UserId, :ratings_CandidacyId, :ratings_CommentId)');
$query->bindValue(':ratings', $ratings, PDO::PARAM_INT);
$query->bindValue(':ratings_UserId', $_SESSION['user']->userId, PDO::PARAM_INT);
$query->bindValue(':ratings_CandidacyId', $getCandidacyId->id, PDO::PARAM_INT);
$query->bindValue(':ratings_CommentId', $getCommentId->id, PDO::PARAM_INT);
if($query->execute())
{
$state = 1;
}
}
}
fclose($handle);
return $state;
}
}
if(extractData($loadedSheetName.'.csv', $cnx) === 1)
{
$_SESSION['flash']['uploadFile'] = "<div class='alert alert-success'>Success !</div>";
header('Location: ../listCandidacies.php');
}
else
{
$_SESSION['flash']['uploadFile'] = "<div class='alert alert-danger'>Error !</div>";
exit(0);
}