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

时间:2013-08-08 16:49:29

标签: php mysql arrays csv

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

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

E.g。

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语句中。

E.g。

    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;

}

1 个答案:

答案 0 :(得分:0)

我从原始数组中删除了“注释”,因为它不包含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','','')