Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将此信息转换为查询?_Php_Mysql_Regex - Fatal编程技术网

Php 如何将此信息转换为查询?

Php 如何将此信息转换为查询?,php,mysql,regex,Php,Mysql,Regex,我有一些信息我想转换成查询,但我不知道这是否可能(使用升华文本上的regex?或其他?) 我在这张表格里有很多汽车品牌和型号的档案 AUSTIN; 1100 希利 迷你 宝马; X1 X3 法拉利 加利福尼亚 插入'car_model'(``、'name_model'、'id_brand`)值(''1100','1')、(''HEALEY','1')、(''MINI','1'); 插入'car_model'(``、'name_model`、'id_brand`)值(''X1','2')、(''

我有一些信息我想转换成查询,但我不知道这是否可能(使用升华文本上的regex?或其他?)

我在这张表格里有很多汽车品牌和型号的档案

AUSTIN;
1100
希利
迷你
宝马;
X1
X3
法拉利
加利福尼亚
插入'car_model'(``、'name_model'、'id_brand`)值(''1100','1')、(''HEALEY','1')、(''MINI','1');
插入'car_model'(``、'name_model`、'id_brand`)值(''X1','2')、(''X3','2');

插入'car_model'(``、'name_model`、'id_brand`)值('''1100','3')既然您将此作为一个PHP问题提问,我将编写一个小PHP脚本来提取模型并将其插入数据库,例如:

<?php
// TODO: add database connection with mysqli_connect()
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
        $sql = "INSERT IGNORE INTO car_model (name_model) VALUES ({$data[0]})";
        mysqli_query($sql);
    }
    fclose($handle);
}
?>

尝试使用
readfile()
,然后使用
explode()
,然后使用几个循环:

$vals   =   'AUSTIN;
1100
HEALEY
MINI
BMW;
X1
X3
FERRARI;
CALIFORNIA
';

    function FetchModels($vals)
        {
            // Explode on new lines
            $set    =   explode("\n",$vals);
            // If the array is set and not empty
            if(is_array($set) && !empty($set)) {
                    // Filter empties
                    $set    =   array_filter($set);
                    // Loop through the array
                    foreach($set as $value) {
                            // Search for the semi-colon which
                            // indicates brand name
                            if(strpos($value,';') !== false)
                                // Trim off semi-colon
                                $brand = trim($value,";");
                            else {
                                // If the brand is set, start models
                                if(isset($brand))
                                    $array[$brand][]    =   $value;
                                }
                        }
                }
            // Return the array if made
            return (isset($array))? $array:false;
        }

    // Create array from function
    $array  =   FetchModels($vals);

    if(is_array($array) && !empty($array)) {
            // Loop through the array and insert into db
            $i = 1;
            foreach($array as $brand => $models) {
                    // Set the start of the sql
                    $sql    =   "insert into `carbrand` (`name_model`,`id_brand`) VALUES";  
                    // Loop trough models
                    foreach($models as $name) {
                            $finals[$brand][]   =   "('$name',$i)";
                        }
                    // Add models to the sql
                    echo
                    $sql    .=  implode(", ",$finals[$brand]);

                    // Insert your mysql insert here
                    $i++;
                }
        }
给你:

// Original Models array 
// FetchModels()
Array
(
    [AUSTIN] => Array
        (
            [0] => 1100
            [1] => HEALEY
            [2] => MINI
        )

    [BMW] => Array
        (
            [0] => X1
            [1] => X3
        )

    [FERRARI] => Array
        (
            [0] => CALIFORNIA
        )

)

// SQL inserts
insert into `carbrand` (`name_model`,`id_brand`) VALUES('1100',1), ('HEALEY',1), ('MINI',1)
insert into `carbrand` (`name_model`,`id_brand`) VALUES('X1',2), ('X3',2)
insert into `carbrand` (`name_model`,`id_brand`) VALUES('CALIFORNIA',3)

您好,谢谢,我试过了,但有一些问题:只插入了带有数字的型号,没有人有id_品牌。非常感谢:)太完美了,再次感谢:)