{
"gstin":"29AAHCS4690J1ZQ",
"fp":"072017",
"b2b":[
{
"ctin":"01AAACJ4126D1ZE",
"cfs":"Y",
"inv":[
{
"itms":[
{
"num":1,
"itc":{
"tx_cs":0,
"elg":"ip",
"tx_i":112.5
},
"itm_det":{
"csamt":0,
"rt":18,
"txval":625,
"iamt":112.5
}
}
],
"val":738,
"inv_typ":"R",
"flag":"N",
"updby":"S",
"pos":"29",
"idt":"31-07-2017",
"rchrg":"N",
"cflag":"U",
"inum":"FL-17000069",
"chksum":"4d3e41facf5002f418e1ac605d9888d74ee21652e19de6db624fc7106e3c5866"
}
]
},
{
"ctin":"29AACFI3027K1ZC",
"cfs":"Y",
"inv":[
{
"itms":[
{
"num":1800,
"itc":{
"elg":"ip",
"tx_c":171.59,
"tx_s":171.59
},
"itm_det":{
"samt":171.59,
"rt":18,
"txval":1906.5,
"camt":171.59
}
}
],
"val":2250,
"inv_typ":"R",
"flag":"N",
"updby":"S",
"pos":"29",
"idt":"06-07-2017",
"rchrg":"Y",
"cflag":"U",
"inum":"0042",
"chksum":"aa1187a2bb7d6098c39c4c992bd391ab7fd59e7eeefcea5b91fc9881ba4423d8"
}
]
}
]
}
上面的代码是JSON,我按照以下方式访问它,如下所示
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db = new PDO("mysql:host=$servername;dbname=item", $username, $password);
$jsondata = file_get_contents('data.json');
$data = json_decode($jsondata, true);
$stmt = $db->prepare("insert into itc(tx_cs, elg) values(? , ?)");
var_dump($data);
// echo $data;
foreach($data as $row){
$stmt->bindParam(1, $row['tx_cs']);
$stmt->bindParam(2, $row['elg']);
$stmt->execute();
}
?>
执行insert语句后,我在mysql中得到的任何解决方案都是null。如果我试图插入非法字符串&#34; tx_cs&#34;如果有任何替代方法可以访问嵌套的JSON数据。而且我想知道数组索引如何提前感谢。
这是var_dump结果array(4) { ["gstin"]=> string(15) "29AAHCS4690J1ZQ" ["fp"]=> string(6) "072017" ["b2b"]=> array(59) { [0]=> array(3) { ["ctin"]=> string(15) "01AAACJ4126D1ZE" ["cfs"]=> string(1) "Y" ["inv"]=> array(1) { [0]=> array(11) { ["itms"]=> array(1) { [0]=> array(3) { ["num"]=> int(1) ["itc"]=> array(3) { ["tx_cs"]=> int(0) ["elg"]=> string(2) "ip" ["tx_i"]=> float(112.5) } ["itm_det"]=> array(4) { ["csamt"]=> int(0) ["rt"]=> int(18) ["txval"]=> int(625) ["iamt"]=> float(112.5) } } } ["val"]=> int(738) ["inv_typ"]=> string(1) "R" ["flag"]=> string(1) "N" ["updby"]=> string(1) "S" ["pos"]=> string(2) "29" ["idt"]=> string(10) "31-07-2017" ["rchrg"]=> string(1) "N" ["cflag"]=> string(1) "U" ["inum"]=> string(11) "FL-17000069" ["chksum"]=> string(64)
答案 0 :(得分:0)
您需要转到itms
数组才能获得所需的密钥,您需要第二个foreach
foreach($data['b2b'] as $inner){
foreach( $inner['inv'][0]['itms'] as $row){
$stmt->bindParam(1, $row['itc']['tx_cs']);
$stmt->bindParam(2, $row['itc']['elg']);
$stmt->execute();
}
}
或:
foreach($data['b2b'] as $inner){
$stmt->bindParam(1, $inner['inv'][0]['itms'][0]['itc']['tx_cs']);
$stmt->bindParam(2, $inner['inv'][0]['itms'][0]['itc']['elg']);
$stmt->execute();
}
答案 1 :(得分:0)
使用以下代码,完美无缺:
$db = new PDO("mysql:host=$servername;dbname=item", $username, $password);
$data = json_decode($jsondata, true);
$sql = 'INSERT INTO itc (tx_cs, elg) VALUES ';
$insertQuery = array();
$insertData = array();
$n = 0;
foreach($data['b2b'] as $inner){
foreach( $inner['inv'][0]['itms'] as $row){
$insertQuery[] = '(:tx_cs' . $n . ', :elg' . $n . ')';
$insertData['tx_cs' . $n] = $row['itc']['tx_cs'];
$insertData['elg' . $n] = $row['itc']['elg'];
$n++;
}
}
if (!empty($insertQuery)) {
$sql .= implode(', ', $insertQuery);
$stmt = $db->prepare($sql);
$stmt->execute($insertData);
}
使用这种方法,您只需运行一次查询并具有良好的I / O性能