JSON_decode数组插入到mysql表中

时间:2016-01-04 00:42:11

标签: php mysql json

我有一个问题,我无法弄清楚..我有原始的json数据并将其转换为数组..现在我无法想象如何将此数据插入mysql。

这是原始数据

{"planes":{"0":{"009992":["B734","ZS-JRD","SFR137","5114bc..","0-42.cb",16275,266,369,"1451847066","","",""],"009AD8":["GLEX","ZS-KDR","ZSKDR","20.9638","-87.2369",43000,326,426,"1451847070","","",""],"00A518":["B734","ZS-OAP","CAW438","-32.593","20.8416",34975,47,474,"1451846310","","MN438",""],"00B024":["A319","ZS-SFJ","SAA422","41d520b","1b510-b",0,284,7,"1451847027","","SA422","PLZ-JNB"],"00B0EE":["A346","ZS-SND","SAA260","3.02dada",".b2--4d",19925,320,459,"1451847064","","SA260","JNB-FRA"],"00B1F0":["A343","ZS-SXB","SAA287","5.2.4a0","545245c",34000,212,432,"1451846434","","SA287","HKG-JNB"],"00B207":["A332","ZS-SXY","SAA222","2--3cda1","21.3c.44",40000,311,460,"1451847055","","SA222","JNB-GRU"],"00B223":["A320","ZS-SZA","SAA367","3-.20ccd",".420.-a",36000,228,429,"1451847074","","SA367","JNB-CPT"],"00B225":["A320","ZS-SZC","z.NO-00B225","2-b34.b1","-d3-.1.",0,95,3,"1451847045","","",""],"00B228":["A320","ZS-SZF","SAA365","3-421a4c","..2ca55",38000,227,432,"1451846134","","SA365","JNB-CPT"],"00B229":["A320","ZS-SZG","SAA146",".34-8739","21-9979",29375,87,436,"1451847061","","SA146","JNB-MPM"],"00B22C":["A320","ZS-SZJ","SAA369","-27.9309","25.9569",36000,228,443,"1451847060","","SA369","JNB-CPT"],"00B23B":["A320","ZS-SZY","z.NO-00B23B","0591.476","5715438",0,104,8,"1451847063","","",""],"00C452":["B738","ZS-ZWB","CAW455","3--2d0db","5c21dad",400,165,134,"1451847074","","MN455","HLA-CPT"],enter code here

我使用json_decode

之后
(
[planes] => stdClass Object
    (
        [0] => stdClass Object
            (
                [009992] => Array
                    (
                        [0] => B734
                        [1] => ZS-JRD
                        [2] => SFR137
                        [3] => 5114bc..
                        [4] => 0-42.cb
                        [5] => 16275
                        [6] => 266
                        [7] => 369
                        [8] => 1451847066
                        [9] => 
                        [10] => 
                        [11] => 
                    )

                [009AD8] => Array
                    (
                        [0] => GLEX
                        [1] => ZS-KDR
                        [2] => ZSKDR
                        [3] => 20.9638
                        [4] => -87.2369
                        [5] => 43000
                        [6] => 326
                        [7] => 426
                        [8] => 1451847070
                        [9] => 
                        [10] => 
                        [11] => 
                    )

                [00A518] => Array
                    (
                        [0] => B734
                        [1] => ZS-OAP
                        [2] => CAW438
                        [3] => -32.593
                        [4] => 20.8416
                        [5] => 34975
                        [6] => 47
                        [7] => 474
                        [8] => 1451846310
                        [9] => 
                        [10] => MN438
                        [11] => 
                    )

                [00B024] => Array
                    (
                        [0] => A319
                        [1] => ZS-SFJ
                        [2] => SAA422
                        [3] => 41d520b
                        [4] => 1b510-b
                        [5] => 0
                        [6] => 284
                        [7] => 7
                        [8] => 1451847027
                        [9] => 
                        [10] => SA422
                        [11] => PLZ-JNB
                    )

                [00B0EE] => Array
                    (
                        [0] => A346
                        [1] => ZS-SND
                        [2] => SAA260
                        [3] => 3.02dada
                        [4] => .b2--4d
                        [5] => 19925
                        [6] => 320
                        [7] => 459
                        [8] => 1451847064
                        [9] => 
                        [10] => SA260
                        [11] => JNB-FRA
                    )

继承我的剧本        //获取JSON文件并将内容解码为PHP数组/值

   $jsonFile = 'update.json';
   $data = json_decode(file_get_contents($jsonFile), true);


 $id = $data['planes'];
$actype = $data['0'];
$tail = $data['1'];
$flight = $data['2'];
$long = $data['3'];
$lat = $data['4'];
$altitude = $data['5'];
$heading = $data['6'];
$gspeed = $data['7'];
$dont = $data['8'];
$dont1 = $data['9'];
$flight2 = $data['10'];
$routing = $data['8'];


   $sql = "INSERT INTO         flight(plane,actype,tail,flight,long,lat,altitude,heading,gspeed,dont,dont1,f light2,routing)VALUES('$plane,$actype,$tail,$flight,$long,$lat,$altitude,$heading,$gspeed,$dont,$dont1,$flight2,$routing')";
  if(!mysql_query($sql,$con))
 {
     die('Error : ' . mysql_error());
 }

1 个答案:

答案 0 :(得分:0)

您只需要遍历刚刚解码的数据:

$data = json_decode($your_json);
// loop
foreach($data['planes'] as $item) {
    foreach($item as $key => $fields){
        $values = "'" . implode("', '", $fields) . "'";
        $sql = "INSERT INTO         flight(plane,actype,tail,flight,long,lat,altitude,heading,gspeed,dont,dont1,f light2,routing)VALUES('$key', $values)";
        if(!mysql_query($sql,$con)) {
            die('Error : ' . mysql_error());
        }
    }
}

以上假设密钥(009992 / 009AD8 / etc ..)是航班号。它还假定数据按您需要的顺序排列。