我使用JSON依赖项,我必须将其插入数据库
这是我的PHP代码
$data = [];
foreach ($quantity as $key3 => $fname1) {
$data['quantity'][] = $_POST['quantity'][$key3];
}
foreach ($flower_id as $key => $fname) {
$data['flower_id'][] = $_POST['flower_id'][$key];
}
foreach ($flower_price as $key1 => $fname1) {
$data['flower_price'][] = $_POST['flower_price'][$key1];
}
foreach ($customer_id as $key2 => $fname1) {
$data['customers'][] = $_POST['customer_id'][$key2];
}
echo json_encode($data);
JSON响应是
{"quantity":["2","3"],"flower_id":["1"],"flower_price":["12"],"customers":["21","20"]}
如果是多朵花,则为单花
{"quantity":["13","2","12","3"],"flower_id":["1","2"],"flower_price":["12","22"],"customers":["21","20"]}
每个客户有两朵花,每朵花的价格和数量如图所示
但是我需要单独的依赖项数组才能插入数据库。 就像个别客户依赖的flower_id flower_price和数量 怎么做
看起来像
[{
"customer_id": 20,
"flower_id": 1,
"flower_price": 12,
"quantity": 3
},
{
"customer_id": 21,
"flower_id": 1,
"flower_price": 12,
"quantity": 2
}]
如果您有其他选择可以支持
对此请帮助我
谢谢