如何从结果集生成嵌套数组结构?

时间:2017-11-02 11:27:58

标签: php nested resultset

我有这个Json数组事件

[
    {
        "id": "4",
        "event_name": "Harliquins 7s",
        "event_description": "Ruggby game",
        "event_date": null,
        "event_venue": "UFA grounds",
        "event_company": "Harliquins",
        "event_image": "http://www.aal-europe.eu/wp-content/uploads/2013/12/events_medium.jpg",
        "event_ticket_no": "200",
        "paybill": "25666",
        "status": "0"
    },
    {
        "id": "5",
        "event_name": "christie &s",
        "event_description": "Ruggby",
        "event_date": "1-2-2917",
        "event_venue": "KISUMU ground",
        "event_company": "Kenya Games",
        "event_image": "N/A",
        "event_ticket_no": "400",
        "paybill": "79000",
        "status": "0"
    }
]

我也有这个选项

[
    {
        "id": "4",
        "event_id": "5",
        "options_id": "1",
        "seasonal": "1",
        "amount": "300"
    },
    {
        "id": "5",
        "event_id": "5",
        "options_id": "2",
        "seasonal": "1",
        "amount": "400"
    }
]

我希望将此作为结果

[
    {
        "id": "4",
        "event_name": "Harliquins 7s",
        "event_description": "Ruggby game",
        "event_date": null,
        "event_venue": "UFA grounds",
        "event_company": "Harliquins",
        "event_image": "http://www.aal-europe.eu/wp-content/uploads/2013/12/events_medium.jpg",
        "event_ticket_no": "200",
        "paybill": "25666",
        "status": "0"
    },
    {
        "id": "5",
        "event_name": "christie &s",
        "event_description": "Ruggby",
        "event_date": "1-2-2917",
        "event_venue": "KISUMU ground",
        "event_company": "Kenya Games",
        "event_image": "N/A",
        "event_ticket_no": "400",
        "paybill": "79000",
        "status": "0",
        "Options:" [
            {
                 "id": "4",
                 "event_id": "5",
                 "options_id": "1",
                 "seasonal": "1",
                 "amount": "300"
             },
             {
                 "id": "5",
                 "event_id": "5",
                 "options_id": "2",
                 "seasonal": "1",
                 "amount": "400"
             }
         ]
    }
]

这是我的代码:

while( $row = $result->fetch_assoc()){
    $new_array[] =$row;
    $new_array['options'] =getTicketOptions($row['id']);
}
echo json_encode($new_array);

每个事件对象都有一个选项数组。

4 个答案:

答案 0 :(得分:0)

你已经快到了。您需要做的就是一次分配$ row和Options:

while( $row = $result->fetch_assoc()){
    $new_array[] = array($row, "Options" => getTicketOptions($row['id']));
}
echo json_encode($new_array);

原因是$new_array[]使用let headers = new Headers(); headers.append('Content-Type', "application/json"); var url = "https://myawssigningurlexample.com/1234"; var params = JSON.stringify({ someObj: 'blah' }); return this._http.post(url, params, { headers: headers }) .map(res => res.json()) .catch((error: any) => { return Observable.throw(error); }); 自动设置新密钥而不知道其值。因此,您不能稍后在此记录中轻松添加内容。同时做两件事都可以解决它。

答案 1 :(得分:0)

在我看来,你只需要合并两个对象:

$mergedObject = (object) array_merge((array) $events, (array) $options);

答案 2 :(得分:0)

我想你想在最后位置添加第二个数组?如果是,则按照回答。

第一个数组

[{"id":"4","event_name":"Harliquins 7s"},{"id":"5","event_name":"Harliquins 7s"}]

第二阵列

[{"id":"4","event_id":"5","options_id":"1"},{"id":"4","event_id":"5","options_id":"1"}]

必需的数组

[{"id":"4","event_name":"Harliquins 7s"},{"id":"5","event_name":"Harliquins 7s","options":[{"id":"4","event_id":"5","options_id":"1"},{"id":"4","event_id":"5","options_id":"1"}]}]

Php Code

$last_key = end(array_keys($data));
        foreach ($data as $key => $value) {
            if($key == $last_key){
                $data[$key]['options'] = $data2;
            }

        }

        echo json_encode($data);

答案 3 :(得分:0)

我使用array_maparray_filter函数获得相同的结果,您可以在此处尝试我的代码:php sandbox,它始终在events数组中创建options属性:

String