我有两个while循环来获取两个不同的值作为数组,它们被称为标题和menu.In我希望像菜单数组值的结果在标题数组中。
这是我的代码:
$heading = array();
$menu = array();
$stmt_heading = $this->conn->prepare("SELECT main_heading_name,sub_heading_name FROM `selected_food_main_sub_heading` WHERE branch_id = ? AND brand_id = ? AND business_id = ? ");
$stmt_heading->bind_param("iii", $branch_id, $brand_id,$business_id);
$stmt_heading->execute();
$result = $stmt_heading->get_result();
while ($row = $result->fetch_assoc())
{
array_push($heading, $row);
$stmt_menu = $this->conn->prepare("SELECT * FROM `menu_deatils` WHERE main_heading_name = ? AND sub_heading_name = ? ");
$stmt_menu->bind_param("ss", $row['main_heading_name'], $row['sub_heading_name']);
$stmt_menu->execute();
$result_menu = $stmt_menu->get_result();
while ($row_menu = $result_menu->fetch_assoc())
{
array_push($menu, $row_menu);
}
$data['menu_list'] = $menu;
array_push($heading, $data);
$stmt_menu->close();
}
return $heading;
标题数组值:
[
{
"main_heading_name": "Quick Bites",
"sub_heading_name": "Biryani & Rice"
},
{
"main_heading_name": "Bites",
"sub_heading_name": "Biryani & Rice"
}
]
菜单数组值:
{
"menu_list": [{
"main_heading_name": "Quick Bites",
"sub_heading_name": "Biryani & Rice"
},
{
"main_heading_name": "Quick Bites",
"sub_heading_name": "Biryani & Rice"
}
]
}
我想要一个结果:
[
{
"main_heading_name": "Quick Bites",
"sub_heading_name": "Biryani & Rice",
"menu_list": [{
"main_heading_name": "Quick Bites",
"sub_heading_name": "Biryani & Rice"
},
{
"main_heading_name": "Quick Bites",
"sub_heading_name": "Biryani & Rice"
}
]
},
{
"main_heading_name": "Bites",
"sub_heading_name": "Biryani & Rice",
"menu_list": [{
"main_heading_name": "Quick Bites",
"sub_heading_name": "Biryani & Rice"
},
{
"main_heading_name": "Quick Bites",
"sub_heading_name": "Biryani & Rice"
}
]
}
]
怎么办?提前谢谢!
答案 0 :(得分:1)
$FullArray1 = $menu + $heading;
$FullArray2 = array_merge($menu, $heading);
这两个都会产生预期的结果。
http://php.net/manual/en/function.array-merge.php
编辑:回复您的评论
array_push($menu, $heading);