用 children 构造 JSON

Constructing JSON with children

我正在尝试构建我的 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...

你需要这样做。

$json[0]['__children'] = array();

您可以遍历数据,检查每个工作编号是否有基础数据集(使用isset()),然后在每个循环中添加子数据...

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);