通过结果PHP的JSON循环

时间:2017-02-03 09:08:22

标签: php json parsing foreach decode

我有一个Json文件' extract.json',我获取循环结果的内容,但我的代码只返回一行,我的脚本==>

$filename  = "extract.json";
$str = file_get_contents($filename);
if($str!=null) {
    $decoded=json_decode($str,true);
    $compteur = 0;
    $compteur =  count($decoded);
    if(is_array($decoded)) {
        foreach($decoded as $value) {
            $var1= $value["val1"];
            $var2 = $value["val2"];
            $var3 = $value["val3"];
            $var4 = $value["val4"];
            $var5 = $value["val5"];....

            $injection_final = "INSERT INTO `Table`(
                `var1`,
                `var2`,
                `var3`,
                `var4`,
                `var5`,)
                VALUES
                ($var1,$var2,$var3,$var4,$var5)";
            $inj = mysqli_query($con1,$injection_final);
        }
    }
}

MY JSON FILE ==>

[{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"},{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"},{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"},{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"},{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"}...]

4 个答案:

答案 0 :(得分:1)

问题在于您的sql,so it should not insert even a single row。你正在使用日期,但没有把它们放在引号中。试试如下:

 $filename  = "extract.json";
    $str = file_get_contents($filename);
    if($str!=null){
        /////.....rest of your code

                 $var1= $value["val1"];
                $var2 = $value["val2"];
                $var3 = $value["val3"];
                $var4 = $value["val4"];
                $var5 = $value["val5"];

                $injection_final = "INSERT INTO Table(var1,var2,var3,var4,var5,)
                VALUES($var1,$var2,'$var3',$var4,'$var5')";
                echo $injection_final."<br>";//use this query to insert

        ......
    }

答案 1 :(得分:1)

<?php

$json_data = '[{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"},{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"},{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"},{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"},{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"}]';
//uncomment the below line to read from file
//$json_data = file_get_contents("extract.json");
$array_data = json_decode($json_data, true);

for ($i = 0; $i < count($array_data); $i++) {
    $table_column_names = "";
    $table_column_values = "";
    foreach ($array_data[$i] as $coulmn_name => $field_value) {
        $table_column_names  .= "`$coulmn_name`, ";
        $table_column_values  .= "'$field_value', ";
    }
    $table_column_names = rtrim($table_column_names, ", ");
    $table_column_values = rtrim($table_column_values, ", ");
    $sql_query = "INSERT INTO `Table` ($table_column_names) VALUES ($table_column_values)";
    //excute query here -- like if i just print the query
    echo $sql_query."\n";
}

这是您需要的......它将执行所有行 输出:

INSERT INTO `Table` (`val1`, `val2`, `val3`, `val4`, `val5`) VALUES ('1', '2', '0000-00-00', '1', '1939-05-08')
INSERT INTO `Table` (`val1`, `val2`, `val3`, `val4`, `val5`) VALUES ('1', '2', '0000-00-00', '1', '1939-05-08')
INSERT INTO `Table` (`val1`, `val2`, `val3`, `val4`, `val5`) VALUES ('1', '2', '0000-00-00', '1', '1939-05-08')
INSERT INTO `Table` (`val1`, `val2`, `val3`, `val4`, `val5`) VALUES ('1', '2', '0000-00-00', '1', '1939-05-08')
INSERT INTO `Table` (`val1`, `val2`, `val3`, `val4`, `val5`) VALUES ('1', '2', '0000-00-00', '1', '1939-05-08')

答案 2 :(得分:1)

这一行的简单改变 来自$var1,$var2,$var3,$var4,$var5)";

('$var1', '$var2', '$var3', '$var4', '$var5')";

答案 3 :(得分:0)

您的JSON数组的格式不正确。有关示例,请参阅以下链接:

http://www.w3schools.com/js/js_json_arrays.asp