gpt4 book ai didi

PHP - 基于另一个数组显示一个数组

转载 作者:行者123 更新时间:2023-11-30 01:24:47 25 4
gpt4 key购买 nike

最终我只是想构建一个将 csv 文件导入 mysql 数据库的页面。

我已成功导入 csv。我已经设置了该页面,以便用户从下拉列表中选择数据库中的哪个字段对应于 csv 中的哪个部分。

例如

student_firstname: [First_Name] 
student_surname: [Last_Name]
student_address1: [Street_1]
etc.

之后我构建了 2 个数组,第一个数组告诉我 mysql 字段和 csv 标题,第二个数组是 csv 文件中的原始数据:

    Array
(
[firstname] => First_Name
[surname] => Last_Name
[address1] => Street_1
[address2] => Street_2
[address3] => City
[address4] => County
[postcode] => Postcode
[telephone] => Mobile_Phone
[notes] => None
)
Array
(
[0] => Array
(
[Username] =>
[Last_Name] => Jacobs
[First_Name] => Adam
[Email] =>
[Password] => password
[Student_Id] =>
[Middle_Name] =>
[Job_Title] =>
[Department] =>
[Company] =>
[Street_1] => 177 The Road
[Street_2] =>
[City] => Exeter
[County] =>
[Postcode] => EX1 1XX
[Country] =>
[Work_Phone] =>
[Home_Phone] =>
[Work_Fax] =>
[Mobile_Phone] => 01111 1112222
[Website] =>
[Role] => Exeter City
)

[1] => Array
(
[Username] =>
[Last_Name] => Miller
[First_Name] => Adam
[Email] =>
[Password] => password
[Student_Id] =>
[Middle_Name] =>
[Job_Title] =>
[Department] =>
[Company] =>
[Street_1] => The White
[Street_2] => 21A Some House
[City] => Creditery
[County] =>
[Postcode] => EX1 1AA
[Country] =>
[Work_Phone] =>
[Home_Phone] =>
[Work_Fax] =>
[Mobile_Phone] => 07111 112222
[Website] =>
[Role] => Exeter City
)
)

我现在要做的就是创建一个字符串,仅将第二个数组中的值插入到第一个数组中定义的 sql 字段中,这样我就可以添加到 mySQL INSERT 语句中。

例如

    INSERT INTO students (student_firstname, student_surname, student_address1, student_address2, student_address3, student_address4, student_postcode, student_telephone, student_notes, student_added) VALUES 
('Adam','Jacobs','177 The Road','','Exeter','','EX1 1XX','01111 1112222','',''),
('Adam','Millar','The White','21A Some House','Creditory','','EX1 1AA','07111 11222','','')

我当前的代码如下,以说明我确实得到了一些解决方案,但现在变得一团糟

if (isset($_POST['file_upload'])){
$uploaded = 1;
$lines = explode("\n", file_get_contents($_FILES["csvfile"]["tmp_name"]));
$head = str_replace(" ","_",str_getcsv(array_shift($lines)));

$array = array();
foreach ($lines as $line) {
$row = array_pad(str_getcsv($line), count($head), '');
$array[] = array_combine(str_replace(" ","_",$head), $row);
}


//echo '<pre>';
//print_r($array);
//echo '</pre>';
session_start();
$_SESSION['csvimport'] = $array;
}
if (isset($_POST['submitrecords'])){
$fields = array(
'firstname' => $_POST['student_firstname'],
'surname' => $_POST['student_surname'],
'address1' => $_POST['student_address1'],
'address2' => $_POST['student_address2'],
'address3' => $_POST['student_address3'],
'address4' => $_POST['student_address4'],
'postcode' => $_POST['student_postcode'],
'telephone' => $_POST['student_telephone'],
'notes' => $_POST['student_notes']);

$array = $_SESSION['csvimport'];
echo '<pre>';
print_r($fields);
print_r($array);
echo '</pre>';

foreach ($fields as $dbfield){
foreach(array_keys($array[0]) as $value){
if ($dbfield == $value){
foreach($array as $key => $value2){
$snippet .= ",('" . $value2[$dbfield] . "','" . $studentid[0] . "','" . $studentid[1] . "')";
}

}



}
}
$snippet = substr($snippet,1);

$insertSQL = "INSERT INTO students (student_firstname, student_surname, student_address1, student_address2, student_address3, student_address4, student_postcode, student_telephone, student_notes, student_added) VALUES $snippet";


echo $insertSQL;

}

最佳答案

我从原始数组中删除了“注释”,因为它不包含 CSV 文件中的字段,但稍后会再次添加:

$fnames = array (
'firstname' => 'First_Name',
'surname' => 'Last_Name',
'address1' => 'Street_1',
'address2' => 'Street_2',
'address3' => 'City',
'address4' => 'County',
'postcode' => 'Postcode',
'telephone' => 'Mobile_Phone');

命名第二个数组 $csv 后,以下代码会生成数据库查询:

$db = array();
$db_cols = array_keys($fnames);
foreach ($csv as $key => $value){
$insx = array_intersect_key($value, array_flip($fnames));
$db[$key] = array_combine($db_cols, $insx);
$db[$key]['notes'] = '';
$db[$key]['added'] = '';
}
if (count($db) > 0){
$query = "INSERT INTO students (student_";
$query .= implode(', student_', array_keys($db[0])).") VALUES \n";
foreach ($db as $key => $value){
$query .= "('".implode("','", $value)."'),\n";
}
$query = rtrim($query, ",\n");
echo $query;
}

输出:

INSERT INTO students (student_firstname, student_surname, student_address1, student_address2, student_address3, student_address4, student_postcode, student_telephone, student_notes, student_added) VALUES 
('Jacobs','Adam','177 The Road','','Exeter','','EX1 1XX','01111 1112222','',''),
('Miller','Adam','The White','21A Some House','Creditery','','EX1 1AA','07111 112222','','')

关于PHP - 基于另一个数组显示一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18131644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com