识别由不同数量的项组成的json对象

identify json object consisting of different number of items

本文关键字:json 对象 识别      更新时间:2023-09-26

这是我的json对象

[  
{  
    "grup":"",
    "barkod":"1234",
    "amount":"1",
    "selling price":"215",
    "cost":"68.69"
},
{  
    "grup":"4444",
    "barkod":"",
    "ammount":"1",
    "selling price":"49.9",
    "cost":"75"
},
{  
    "grup":"3333",
    "barkod":"091986 ",
    "amount":"1",
    "selling price":"69.9",
    "cost":"16.56"
},
{  
    "cost":"300",
    "pay type":"cash",
    "taksit":"1"
},
{  
    "cost":"200",
    "pay type":"BANK",
    "taksit":"7"
},
{  
    "idNo":""
}
]

这个对象是通过ajax php服务器发送的。然后,将其转换为php数组:

 $datax = json_decode(file_get_contents("php://input"));

我想要前3个元素(数量可以改变)运行CCD_ 2的插入语句,对于4。和5。元素为table2运行插入语句,依此类推

如果你想打印第一个对象,你应该使用这个:

print_r(datax[0]);

我在这里为MySQL编写了一个示例语句,所以您可以为自己的代码编辑它。

$grup = $datax[0]->grup;
$firma_barkod = $datax[0]->firma_barkod;
$adet = $datax[0]->adet;
$birim_satis_fiyati = $datax[0]->birim_satis_fiyati;
$birim_maliyet = $datax[0]->birim_maliyet;
$query = "INSERT INTO table_name (grup, firma_barkod, adet, birim_satis_fiyati, birim_maliyet)
VALUES ('$grup', '$firma_barkod', '$adet', '$birim_satis_fiyati', '$birim_maliyet');

我只是想让你理解其中的逻辑;如果要到达第一个元素,则应使用$datax[0],如果要到达第四个元素,应使用$datax[3]