[我正在尝试构造自己的JSON,使其看起来像... __children: [
之后紧随Qty Shipped: null,
[
{
'Job Number': '22983321',
'LN #': null,
'Description': null,
'Qty': null,
'AS400 Ship Date': null,
'Date Showed on Report': null,
'Days to Manufacture': null,
'Notes': null,
'Date Shown Completed': null,
'Actual Ship Date': null,
'Qty Shipped': null,
__children: [
{
//values here
},
]
},
]
使用我当前的代码,我得到的是:
{
"0": {
"Job Number": "22359501",
"LN #": null,
"Description": null,
"Qty": null,
"AS400 Ship Date": null,
"Date Showed on Report": null,
"Days to Manufacture": null,
"Notes": null,
"Date Shown Completed": null,
"Actual Ship Date": null,
"Qty Shipped": null
},
"_children": [
{
//values here
},
]
}
这是我当前的代码:
$checkJob = '';
//output data into spreadsheet view
while($row = $stmt->fetch(PDO::FETCH_BOUND)) {
if ($checkJob == $job_number) {
//print "<b>checkJob: " . $checkJob . " | " . "currentJob: " . $job_number . "</b><br>";
$json['__children'][] = array(
'LN #' => $line_item,
'Description' => $description,
'Qty' => $qty,
'AS400 Ship Date' => $as400_ship_date,
'Date Showed on Report' => $date_showed_on_report,
'Days to Manufacture' => "5",
'Notes' => $notes,
'Date Shown Completed' => $date_shown_complete,
'Actual Ship Date' => $actual_ship_date,
'Qty Shipped' => $qty_shipped
);
}
else {
$checkJob = $job_number;
//print "checkJob: " . $checkJob . " | " . "currentJob: " . $job_number . "<br>";
$json[] = array(
'Job Number' => $job_number,
'LN #' => null,
'Description' => null,
'Qty' => null,
'AS400 Ship Date' => null,
'Date Showed on Report' => null,
'Days to Manufacture' => null,
'Notes' => null,
'Date Shown Completed' => null,
'Actual Ship Date' => null,
'Qty Shipped' => null,
);
}
}
我尝试将$json[][] = array(
更改为$json['__children'][] = array(
,但仍无法正确构造JSON。我正在尝试使用建议的内置函数来构建JSON ...
while($row = $stmt->fetch(PDO::FETCH_BOUND)) {
if ( !isset($json[$job_number])) {
$json[$job_number] = array(
'Job Number' => $job_number,
'LN #' => null,
'Description' => null,
'Qty' => null,
'AS400 Ship Date' => null,
'Date Showed on Report' => null,
'Days to Manufacture' => null,
'Notes' => null,
'Date Shown Completed' => null,
'Actual Ship Date' => null,
'Qty Shipped' => null,
);
}
$json[$job_number]['__children'][] = array(
'LN #' => $line_item,
'Description' => $description,
'Qty' => $qty,
'AS400 Ship Date' => $as400_ship_date,
'Date Showed on Report' => $date_showed_on_report,
'Days to Manufacture' => "5",
'Notes' => $notes,
'Date Shown Completed' => $date_shown_complete,
'Actual Ship Date' => $actual_ship_date,
'Qty Shipped' => $qty_shipped
);
}
}
// Remove job number keys
$json = array_values($json);
$json[0]['__children'] = array();
while($row = $stmt->fetch(PDO::FETCH_BOUND)) {
if ( !isset($json[$job_number])) {
$json[$job_number] = array(
'Job Number' => $job_number,
'LN #' => null,
'Description' => null,
'Qty' => null,
'AS400 Ship Date' => null,
'Date Showed on Report' => null,
'Days to Manufacture' => null,
'Notes' => null,
'Date Shown Completed' => null,
'Actual Ship Date' => null,
'Qty Shipped' => null,
);
}
$json[$job_number]['__children'][] = array(
'LN #' => $line_item,
'Description' => $description,
'Qty' => $qty,
'AS400 Ship Date' => $as400_ship_date,
'Date Showed on Report' => $date_showed_on_report,
'Days to Manufacture' => "5",
'Notes' => $notes,
'Date Shown Completed' => $date_shown_complete,
'Actual Ship Date' => $actual_ship_date,
'Qty Shipped' => $qty_shipped
);
}
}
// Remove job number keys
$json = array_values($json);