如何设置数组以生成此 JSON 编码格式


how to setup arrays to result to this json encoded format

如何

设置我的数组,当我调用json_encode时,它会返回这样的结果?

{
"rows":[
    {"id":1,"name":"Chai","price":18.00},
    {"id":2,"name":"Chang","price":19.00},
    {"id":3,"name":"Aniseed Syrup","price":10.00},
    {"id":4,"name":"Chef Anton's Cajun Seasoning","price":22.00},
    {"id":5,"name":"Chef Anton's Gumbo Mix","price":21.35},
    {"id":6,"name":"Grandma's Boysenberry Spread","price":25.00},
    {"id":7,"name":"Uncle Bob's Organic Dried Pears","price":30.00},
    {"id":8,"name":"Northwoods Cranberry Sauce","price":40.00},
    {"id":9,"name":"Mishi Kobe Niku","price":97.00}
],
"footer":[
    {"name":"Total","price":282.35}
]
}

我需要获取 2 个结果集,一个用于实际行,一个用于所有行的总和。我将它们转换为数组并将它们组合成 json 以编码为 json,以便在网页上使用。

但最后我想在用 json 编码时以这种方式格式化它们,它会返回上面的格式。但是当我尝试连接它们时,数组似乎更深了一个维度。而这不能被easyUI读取。

json_encode([
    'rows' => [
        ["id" => 1, "name" => "Chai", "price" => 18.00],
        ["id" => 2, "name" => "Chang", "price" => 19.00],
        ["id" => 3, "name" => "Aniseed Syrup", "price" => 10.00],
        ["id" => 4, "name" => "Chef Anton's Cajun Seasoning", "price" => 22.00],
        ["id" => 5, "name" => "Chef Anton's Gumbo Mix", "price" => 21.35],
        ["id" => 6, "name" => "Grandma's Boysenberry Spread", "price" => 25.00],
        ["id" => 7, "name" => "Uncle Bob's Organic Dried Pears", "price" => 30.00],
        ["id" => 8, "name" => "Northwoods Cranberry Sauce", "price" => 40.00],
        ["id" => 9, "name" => "Mishi Kobe Niku", "price" => 97.00]
    ],
    'footer' => [
        [
            'name' => 'Total',
            'price' => 282.35
        ]
    ]
]);

这将为您提供所需的结构,尽管页脚似乎不需要像您在问题中那样是一个数组,我认为这就是您所说的比必要更深的一维的意思,在这种情况下,该部分将是:

    'footer' => [
        'name' => 'Total',
        'price' => 282.35
    ]