【问题标题】:PHP - Displaying one array based on another arrayPHP - 基于另一个数组显示一个数组
【发布时间】:2013-08-08 16:49:28
【问题描述】:

最终,我只是想构建一个将 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;

}

【问题讨论】:

    标签: php mysql arrays csv


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-22
      • 2020-01-04
      • 2019-05-02
      • 2016-03-28
      相关资源
      最近更新 更多