如何在php中访问json嵌套数组数据

This my JSON Data

{
"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"
        }
     ]
  },
  {
     "ctin":"29AAACT7966R2Z6",
     "cfs":"Y",
     "inv":[
        {
           "itms":[
              {
                 "num":1,
                 "itc":{
                    "elg":"ip",
                    "tx_c":20,
                    "tx_s":20
                 },
                 "itm_det":{
                    "samt":20,
                    "rt":5,
                    "txval":800,
                    "camt":20
                 }
              }
           ],
           "val":800,
           "inv_typ":"R",
           "flag":"N",
           "updby":"S",
           "pos":"29",
           "idt":"06-07-2017",
           "rchrg":"Y",
           "cflag":"U",
           "inum":"507002834",
           "chksum":"64b19ecd192545bcfeac89f9ef7e30965122d5159be6a66d4c7888a8b6e1b6ae"
        },
        {
           "itms":[
              {
                 "num":1,
                 "itc":{
                    "elg":"ip",
                    "tx_c":20.38,
                    "tx_s":20.38
                 },
                 "itm_det":{
                    "samt":20.38,
                    "rt":5,
                    "txval":815,
                    "camt":20.38
                 }
              }
           ],
           "val":815,
           "inv_typ":"R",
           "flag":"N",
           "updby":"S",
           "pos":"29",
           "idt":"26-07-2017",
           "rchrg":"Y",
           "cflag":"U",
           "inum":"507015095",
           "chksum":"c76544937d5f7b8513bb64d45ba5708ff4f67197652b6ba06a605931b7ce58b6"
        }
     ]
  },
  {
     "ctin":"29AACCM4309H1ZI",
     "cfs":"Y",
     "inv":[
        {
           "itms":[
              {
                 "num":1,
                 "itc":{
                    "tx_cs":0,
                    "elg":"ip",
                    "tx_c":329,
                    "tx_s":329
                 },
                 "itm_det":{
                    "samt":329,
                    "csamt":0,
                    "rt":28,
                    "txval":2350,
                    "camt":329
                 }
              }
           ],
           "val":3008,
           "inv_typ":"R",
           "flag":"N",
           "updby":"S",
           "pos":"29",
           "idt":"11-07-2017",
           "rchrg":"N",
           "cflag":"U",
           "inum":"021/RS/17000016",
           "chksum":"096e525d241d942cdf7695976d1dfc0ada181ab01d85dc6d6506d4865983a491"
        },
        {
           "itms":[
              {
                 "num":1,
                 "itc":{
                    "tx_cs":0,
                    "elg":"ip",
                    "tx_c":452.38,
                    "tx_s":452.38
                 },
                 "itm_det":{
                    "samt":452.38,
                    "csamt":0,
                    "rt":28,
                    "txval":3231.25,
                    "camt":452.38
                 }
              }
           ],
           "val":4136.01,
           "inv_typ":"R",
           "flag":"N",
           "updby":"S",
           "pos":"29",
           "idt":"21-07-2017",
           "rchrg":"N",
           "cflag":"U",
           "inum":"021/RS/17000026",
           "chksum":"196ff72d3507ea9fe69448cce2df27f9f3bf6ef05c2b691e47f245cb10f53747"
        }
     ]
  },
  {
     "ctin":"29ABRPH6549P1ZK",
     "cfs":"Y",
     "inv":[
        {
           "itms":[
              {
                 "num":1800,
                 "itc":{
                    "elg":"ip",
                    "tx_c":114.39,
                    "tx_s":114.39
                 },
                 "itm_det":{
                    "samt":114.39,
                    "rt":18,
                    "txval":1271,
                    "camt":114.39
                 }
              }
           ],
           "val":1500,
           "inv_typ":"R",
           "flag":"N",
           "updby":"S",
           "pos":"29",
           "idt":"31-07-2017",
           "rchrg":"N",
           "cflag":"U",
           "inum":"10",
           "chksum":"5e2c5b651c027f5707fddd422de3301e71d3a713169c9de00da7de688742b030"
        },
        {
           "itms":[
              {
                 "num":500,
                 "itc":{
                    "elg":"ip",
                    "tx_c":132.5,
                    "tx_s":132.5
                 },
                 "itm_det":{
                    "samt":132.5,
                    "rt":5,
                    "txval":5300,
                    "camt":132.5
                 }
              },
              {
                 "num":1200,
                 "itc":{
                    "elg":"ip",
                    "tx_c":24,
                    "tx_s":24
                 },
                 "itm_det":{
                    "samt":24,
                    "rt":12,
                    "txval":400,
                    "camt":24
                 }
              }
           ],
           "val":6013,
           "inv_typ":"R",
           "flag":"N",
           "updby":"S",
           "pos":"29",
           "idt":"24-07-2017",
           "rchrg":"N",
           "cflag":"U",
           "inum":"9",
           "chksum":"8aa3acd295c97aad72b174bfd98a521914e5c869608cafbc1f07ed1472f269ca"
        }
     ]
  },
  {
     "ctin":"29AAXFM9455A1ZO",
     "cfs":"Y",
     "inv":[
        {
           "itms":[
              {
                 "num":1,
                 "itc":{
                    "elg":"ip",
                    "tx_c":385.56,
                    "tx_s":385.56
                 },
                 "itm_det":{
                    "samt":385.56,
                    "rt":28,
                    "txval":2754,
                    "camt":385.56
                 }
              }
           ],
           "val":3526,
           "inv_typ":"R",
           "flag":"N",
           "updby":"S",
           "pos":"29",
           "idt":"28-07-2017",
           "rchrg":"N",
           "cflag":"U",
           "inum":"1718/508",
           "chksum":"03bbeb45778a4ea3019d7c7a2a71d2d3e48ed2433fe4e027606fb2dba59d4a64"
        }
     ]
  },
  {
     "ctin":"29AAACE3688F1ZG",
     "cfs":"Y",
     "inv":[
        {
           "itms":[
              {
                 "num":1,
                 "itc":{
                    "elg":"ip",
                    "tx_c":882.42,
                    "tx_s":882.42
                 },
                 "itm_det":{
                    "samt":882.42,
                    "rt":28,
                    "txval":6303,
                    "camt":882.42
                 }
              }
           ],
           "val":8068,
           "inv_typ":"R",
           "flag":"N",
           "updby":"S",
           "pos":"29",
           "idt":"19-07-2017",
           "rchrg":"N",
           "cflag":"U",
           "inum":"EEPLB1718/1208",
           "chksum":"7afd73b6b9aeada9113b54ec7151ba08fb2ca0a8e23fc89ac3126d8c9722ff66"
        },
        {
           "itms":[
              {
                 "num":1,
                 "itc":{
                    "elg":"ip",
                    "tx_c":1137.08,
                    "tx_s":1137.08
                 },
                 "itm_det":{
                    "samt":1137.08,
                    "rt":28,
                    "txval":8122,
                    "camt":1137.08
                 }
              }
           ],
           "val":10396,
           "inv_typ":"R",
           "flag":"N",
           "updby":"S",
           "pos":"29",
           "idt":"24-07-2017",
           "rchrg":"N",
           "cflag":"U",
           "inum":"EEPLB1718/1267",
           "chksum":"5079a0d6c289ee0077fb2fd167fd88691f2a1557cff0d2789912bc9460d35f98"
        },
        {
           "itms":[
              {
                 "num":1,
                 "itc":{
                    "elg":"ip",
                    "tx_c":549.36,
                    "tx_s":549.36
                 },
                 "itm_det":{
                    "samt":549.36,
                    "rt":28,
                    "txval":3924,
                    "camt":549.36
                 }
              }
           ],
           "val":5023,
           "inv_typ":"R",
           "flag":"N",
           "updby":"S",
           "pos":"29",
           "idt":"26-07-2017",
           "rchrg":"N",
           "cflag":"U",
           "inum":"EEPLB1718/1289",
           "chksum":"6480112156a400f0ca2380c577c7a59c7a4321da5e845f577be992b6324219ee"
        },
        {
           "itms":[
              {
                 "num":1,
                 "itc":{
                    "elg":"ip",
                    "tx_c":888.16,
                    "tx_s":888.16
                 },
                 "itm_det":{
                    "samt":888.16,
                    "rt":28,
                    "txval":6344,
                    "camt":888.16
                 }
              }
           ],
           "val":8120,
           "inv_typ":"R",
           "flag":"N",
           "updby":"S",
           "pos":"29",
           "idt":"28-07-2017",
           "rchrg":"N",
           "cflag":"U",
           "inum":"EEPLB1718/1322",
           "chksum":"4d0f2654fd0c1c1bf0c1a2fceb18adeb74123ec62d2556f569f22372f9ece4e1"
        },
        {
           "itms":[
              {
                 "num":1,
                 "itc":{
                    "elg":"ip",
                    "tx_c":602.98,
                    "tx_s":602.98
                 },
                 "itm_det":{
                    "samt":602.98,
                    "rt":28,
                    "txval":4307,
                    "camt":602.98
                 }
              }
           ],
           "val":5513,
           "inv_typ":"R",
           "flag":"N",
           "updby":"S",
           "pos":"29",
           "idt":"29-07-2017",
           "rchrg":"N",
           "cflag":"U",
           "inum":"EEPLB1718/1323",
           "chksum":"a3f0dba67b1352affd1877d45a9eabfe99a60f67cda693881c24ab1f2c967a6d"
        },
        {
           "itms":[
              {
                 "num":1,
                 "itc":{
                    "elg":"ip",
                    "tx_c":939.68,
                    "tx_s":939.68
                 },
                 "itm_det":{
                    "samt":939.68,
                    "rt":28,
                    "txval":6712,
                    "camt":939.68
                 }
              }
           ],
           "val":8591,
           "inv_typ":"R",
           "flag":"N",
           "updby":"S",
           "pos":"29",
           "idt":"29-07-2017",
           "rchrg":"N",
           "cflag":"U",
           "inum":"EEPLB1718/1324",
           "chksum":"77f0c25a4b574129b882395d5a79955cfe23c80cd7edf9accef0afd798696478"
        }
     ]
  }
]

This is my PHP code

<?php
$servername = "localhost";
$username = "root";
$password = "";
$db = "item";

$conn = new mysqli($servername, $username, $password, $db);
$jsondata = file_get_contents( $_FILES['file']['tmp_name']);
$data  = json_decode($jsondata, true);
$gstin = $data['gstin'];
$fp = $data['fp'];
$stmt = "INSERT INTO gstin (gstin, fp) VALUES('$gstin', '$fp')";

    mysqli_query($conn, $stmt);

foreach ($data['b2b'] as $row1) {

 $ctin = $row1['ctin'];
 $cfs  = $row1['cfs'];
 $stmt = "INSERT INTO b2b (ctin, cfs) VALUES('$ctin', '$cfs')";

    mysqli_query($conn, $stmt);




foreach($row1['inv'] as $row3){
    $val = $row3['val'];
    $inv_typ = $row3['inv_typ'];
    $flag = $row3['flag'];
    $updby = $row3['updby'];
    $pos = $row3['pos'];
    $idt = $row3['idt'];
    $rchrg = $row3['rchrg'];
    $cflag = $row3['cflag'];
    $inum = $row3['inum'];
    $chksum = $row3['chksum'];

$stmt = "INSERT INTO inv (val, inv_typ, flag, updby, pos, idt, rchrg, cflag, 
inum, chksum) VALUES('$val', '$inv_typ', '$flag', '$updby', '$pos', '$idt', 
'$rchrg', '$cflag', '$inum', '$chksum')";

$result = mysqli_query($conn, $stmt);
foreach($row3['inv'][0]['itms'] as $row2){

 (isset($row2['itc']['tx_cs'])) ? $tx_cs=$row2['itc']['tx_cs'] : null;
 $elg =  $row2['itc']['elg'];
 (isset($row2['itc']['tx_i'])) ? $tx_i = $row2['itc']['tx_i'] : null;
 (isset($row2['itc']['tx_c'])) ? $tx_c =$row2['itc']['tx_c'] : null;
 (isset($row2['itc']['tx_s'])) ? $tx_s = $row2['itc']['tx_s'] : null;
 (isset($row2['itm_det']['samt'])) ?  $samt = $row2['itm_det']['samt'] : 
 null;
 (isset($row2['itm_det']['csamt'])) ? $csamt = $row2['itm_det']['csamt'] : 
 null;
 (isset($row2['itm_det']['iamt'])) ? $iamt = $row2['itm_det']['iamt'] : 
 null;
 (isset($row2['itm_det']['camt'])) ? $camt = $row2['itm_det']['camt'] : 
 null;

 $rt =   $row2['itm_det']['rt'];
 $txval = $row2['itm_det']['txval'];


$stmt = "INSERT INTO itms (tx_cs, elg, tx_i, tx_c, tx_s, samt, csamt, rt, 
txval, camt, iamt) VALUES('$tx_cs', '$elg', '$tx_i', '$tx_c', '$tx_s', 
'$samt', '$csamt', '$rt', '$txval', '$camt', '$iamt')";

$result = mysqli_query($conn, $stmt);

}
}
}

In this JSON data I am getting correct values in array [b2b] and in [inv] but I am not getting [itms] values correctly means if I iterate itms array that time if ctin contains 10 itms that time the foreach loop is iterating only first itms foreach loop not looping deeply for that reason duplicate values are inserting into the database. thanks in advance

dongyou6847
dongyou6847 对不起,因为没有当前,我会尝试
2 年多之前 回复
dongyou5098
dongyou5098 不应该是$row3['itms'][0]?
2 年多之前 回复
duanpu2272
duanpu2272 如果我使用1那个时间值没有插入数据库
2 年多之前 回复
duandian2725
duandian2725 数组从第0个元素(索引)开始,这就是为什么。
2 年多之前 回复
dtuct88226
dtuct88226 foreach($row3['inv'][0]['itms']as$row2){-在第三个foreach中你为什么要寻找第0个元素?
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问