我的json:
protected alpha
我现在所拥有的:
{
"orders": [
{
"id": 10,
"number": 10,
"line_items": [
{
"id": 3,
"sub": "21.00",
"sub_tax": "0.00",
"total": "21.00",
"total_tax": "0.00",
"quantity": 1,
"tax_class": null,
"meta": []
}
]
}
]
}
foreach的结束我将这些数据插入数据库中。这里的问题如下:
如果有多个line_items,则应在数据库中创建新行。仅插入:$json = json_decode(file_get_contents(JSON_URL), true);
foreach ($json as $u => $z){
foreach ($z as $n => $line){
$number = $line['number'];
$line_item_id = $line['line_items'][0]['id'];
$line_item_sub = $line['line_items'][0]['sub];
$line_item_sub_tax = $line['line_items'][0]['sub_tax'];
$line_item_total = $line['line_items'][0]['total'];
$line_item_total_tax = $line['line_items'][0]['total_tax'];
$line_item_quantity = $line['line_items'][0]['quantity'];
$line_item_tax_class = $line['line_items'][0]['tax_class'];
}
}
和其他number
。
请记住,可以说,创建了20个不同的数据库行,其中只有一个line_items
,但只有两个具有多个line_items
数组。我怎么能做这个工作?
我尝试过这样的事情:
line_item
但我不这么认为这可行。你有什么想法我能做到这一点吗? 如果我能提供更多信息,请告诉我。谢谢!
我如何插入数据:
$countid = $line['line_items'][0]['id'];
if(count($countid) > 1){
$number = $line['order_number'];
$line_item_id = $line['line_items'][0]['id'];
$line_item_sub = $line['line_items'][0]['sub'];
$line_item_sub_tax = $line['line_items'][0]['sub_tax'];
$line_item_total = $line['line_items'][0]['total'];
$line_item_total_tax = $line['line_items'][0]['total_tax'];
$line_item_quantity = $line['line_items'][0]['quantity'];
$line_item_tax_class = $line['line_items'][0]['tax_class'];
} else {
<!-- Insert data normally -->
foreach循环抓住每行约45个不同的项目。其中12个来自$con = mysqli_connect("111.11.111.11","username","password","database") or die('Could not connect: ' . mysql_error());
// prepare your insert query
$stmt = mysqli_prepare($con, 'INSERT INTO table_name( values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names, values_names,) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
// bind the upcoming variable names to the query statement
mysqli_stmt_bind_param($stmt, 'isssssssssssssssssssssssssssssssssssssssssssss', $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables $values_variables);
。在第二个foreach循环结束时,有line_items
并且这很好。需要帮助来解决我应该找到if语句或另一个foreach循环的逻辑,这样每当当前mysqli_stmt_execute($stmt);
下有多个line_items
数组时,我就可以创建新行。
答案 0 :(得分:0)
根据您提供的JSON字符串,我假设此代码可以解决您的问题:
$json = json_decode(file_get_contents(JSON_URL), true);
foreach ($json['orders'] as $line) {
foreach ($line['line_items'] as $item) {
// no need to distinguish between 1 or more items
// foreach handles both cases
$number = $line['number'];
$line_item_id = $item['id'];
$line_item_sub = $item['sub'];
$line_item_sub_tax = $item['sub_tax'];
$line_item_total = $item['total'];
$line_item_total_tax = $item['total_tax'];
$line_item_quantity = $item['quantity'];
$line_item_tax_class = $item['tax_class'];
mysqli_stmt_bind_param($stmt, 'isssssssssssssssssssssssssssssssssssssssssssss', $number, $line_item_id, $line_item_sub, ...);
}
}