如何从结果集中生成嵌套数组结构?
How to generate a nested array structure from a 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);
每个事件对象都有一个选项数组。
你快到了。您需要做的就是立即分配 $row 和 Options:
while( $row = $result->fetch_assoc()){
$new_array[] = array($row, "Options" => getTicketOptions($row['id']));
}
echo json_encode($new_array);
原因是使用 $new_array[]
时您会自动设置一个新键而不知道它的值。因此,您以后不能轻易地向该记录中添加内容。两者同时进行即可为您解决。
在我看来你只需要合并 2 个对象:
$mergedObject = (object) array_merge((array) $events, (array) $options);
我想你想在最后一个位置添加第二个数组??如果是,则按照答案进行操作。
第一个数组
[{"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代码
$last_key = end(array_keys($data));
foreach ($data as $key => $value) {
if($key == $last_key){
$data[$key]['options'] = $data2;
}
}
echo json_encode($data);
我使用 array_map and array_filter functions, You can try my code here: php sandbox 得到相同的结果,它总是在事件数组中创建选项 属性:
$events = json_decode($events, true);
$options = json_decode($options, true);
$result = array_map( function ($item) use ($options) {
$item['options'] = array_filter($options, function ($option) use ($item) {
return $item['id'] == $option['event_id'];
});
return $item;
}, $events);
print_r($result);
我有这个 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);
每个事件对象都有一个选项数组。
你快到了。您需要做的就是立即分配 $row 和 Options:
while( $row = $result->fetch_assoc()){
$new_array[] = array($row, "Options" => getTicketOptions($row['id']));
}
echo json_encode($new_array);
原因是使用 $new_array[]
时您会自动设置一个新键而不知道它的值。因此,您以后不能轻易地向该记录中添加内容。两者同时进行即可为您解决。
在我看来你只需要合并 2 个对象:
$mergedObject = (object) array_merge((array) $events, (array) $options);
我想你想在最后一个位置添加第二个数组??如果是,则按照答案进行操作。
第一个数组
[{"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代码
$last_key = end(array_keys($data));
foreach ($data as $key => $value) {
if($key == $last_key){
$data[$key]['options'] = $data2;
}
}
echo json_encode($data);
我使用 array_map and array_filter functions, You can try my code here: php sandbox 得到相同的结果,它总是在事件数组中创建选项 属性:
$events = json_decode($events, true);
$options = json_decode($options, true);
$result = array_map( function ($item) use ($options) {
$item['options'] = array_filter($options, function ($option) use ($item) {
return $item['id'] == $option['event_id'];
});
return $item;
}, $events);
print_r($result);