Php 组合具有相同值的数组,但将另一个数组字典添加到组合数组中
我有以下PHP代码来获取mysql数据:Php 组合具有相同值的数组,但将另一个数组字典添加到组合数组中,php,pdo,Php,Pdo,我有以下PHP代码来获取mysql数据: while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { // create an array for the results $menuItems['menu_items'][] = array( 'item_id' => $row['item_id'], 'rest_id' => $row['rest_id'],
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
// create an array for the results
$menuItems['menu_items'][] = array(
'item_id' => $row['item_id'],
'rest_id' => $row['rest_id'],
'item_name' => $row['item_name'],
'item_genre' => $row['item_genre'],
'item_price' => $row['item_price'],
'item_descript' => $row['item_descript'],
'wait_time' => $row['wait_time'],
'ingredients' => array_filter(array(// do not display ingredients if null
'ingredient_id' => $row['ingredient_id'],
'ingredient_name' => $row['ingredient_name'],
'ingredient_price' => $row['ingredient_price'],
'ingredient_default' => $row['ingredient_default']
))
);
// print success or no error
$menuItems['error'] .= '';
}
// check if any menu items exists
if( $menuItems != null ) {
echo json_encode($menuItems);
print_r($menuItems);
}
else {
echo json_encode(array('error' => 'There are currently no menu items for this location'));
}
这是JSON输出数组,但它为与其关联的每个成分输出相同的菜单项:
Array
(
[menu_items] => Array
(
[0] => Array
(
[item_id] => 102
[rest_id] => 67
[item_name] => Tilapia And Rice
[item_genre] => Fish %26 Seafood
[item_price] => 9.50
[item_descript] => Tilapia and rice
[wait_time] => 45
[ingredients] => Array
(
[ingredient_id] => 1
[ingredient_name] => herbs
[ingredient_price] => 0.50
)
)
[1] => Array
(
[item_id] => 102
[rest_id] => 67
[item_name] => Tilapia And Rice
[item_genre] => Fish %26 Seafood
[item_price] => 9.50
[item_descript] => Tilapia and rice
[wait_time] => 45
[ingredients] => Array
(
[ingredient_id] => 2
[ingredient_name] => lemon
[ingredient_price] => 0.00
[ingredient_default] => 1
)
)
[2] => Array
(
[item_id] => 105
[rest_id] => 67
[item_name] => Ninja Roll
[item_genre] => Japanese
[item_price] => 8.00
[item_descript] => Sushi roll. 6-8 pieces. Captain's orders!
[wait_time] => 30
[ingredients] => Array
(
)
)
[3] => Array
(
[item_id] => 106
[rest_id] => 67
[item_name] => Sushi
[item_genre] => Japanese
[item_price] => 8.00
[item_descript] => Menu item description (optional)
[wait_time] => 30
[ingredients] => Array
(
)
)
)
[error] =>
)
不过,我需要以下内容。将配料合并到相关菜单项中
Array
(
[menu_items] => Array
(
[0] => Array
(
[item_id] => 102
[rest_id] => 67
[item_name] => Tilapia And Rice
[item_genre] => Fish %26 Seafood
[item_price] => 9.50
[item_descript] => Tilapia and rice
[wait_time] => 45
[ingredients] => Array
[0] => Array
(
[ingredient_id] => 1
[ingredient_name] => herbs
[ingredient_price] => 0.50
)
[1] => Array
(
[ingredient_id] => 2
[ingredient_name] => lemon
[ingredient_price] => 0.00
[ingredient_default] => 1
)
)
[1] => Array
(
[item_id] => 105
[rest_id] => 67
[item_name] => Ninja Roll
[item_genre] => Japanese
[item_price] => 8.00
[item_descript] => Sushi roll. 6-8 pieces. Captain's orders!
[wait_time] => 30
[ingredients] => Array
(
)
)
[2] => Array
(
[item_id] => 106
[rest_id] => 67
[item_name] => Sushi
[item_genre] => Japanese
[item_price] => 8.00
[item_descript] => Menu item description (optional)
[wait_time] => 30
[ingredients] => Array
(
)
)
)
[error] =>
)
如何创建所需的输出阵列?我太糊涂了,弄不懂这些 使用关联数组。当您控制索引时,生活会变得简单得多。这在处理一对多数据库结果时尤其如此,正如您所做的那样 例如(我显然在对您的模型进行假设/猜测): 在上面的示例中,您创建了一个菜单列表,其中menu.id是数组键。每个菜单都有一个值“配料”,它同样是一个数组。在我的示例中,我使用ingred_id作为键和值。显然,您需要修改代码,将ingred_id用作键,并将其他成分信息数组用作值
请注意,根据数据库的大小,运行多个查询可能会更高效。现在,每行都有大量冗余数据。我认为你是对的。。。我早就想到了。可能更容易运行两个查询,一个用于填充菜单项,另一个用于填充配料,并将配料数组添加到关联的菜单项。在花费了相当长的时间后,不确定这是否是我一直在寻找的,但你让我尝试了新的东西,我更新了我的问题,我现在有更多的成分标签,如果这有助于为你提供解决这个问题所需的更多信息。。。只连接表似乎比运行多个查询更好。。。我必须把这个项目排好,因为这是每个成员的外键。还有其他想法吗?我进一步澄清了我的问题好吧,我终于花了两天时间玩关联数组并找到了如何实现您的答案。这是可行的,但我觉得这不是我所做的最好的实践,那么有什么更好的方法来解决这个问题呢?谢谢你的帮助,它终于按照我想要的方式工作了,但是我对你关于效率的评论很感兴趣。
if ($stmt = $dbh->prepare($query)) {
// initialise an array for the results
$menuItems = array();
if ( $stmt->execute(array($rest_id)) ) {
while ($row = $stmt->fetchAll(PDO::FETCH_ASSOC)) {
// Have we seen this menu before? If not, add it to the array
if ( !isset($menuItems['menu_items'][$row['id']]) ) {
$menuItems['menu_items'][$row['id']] = array(ingredients => array());
}
// Add the ingredient.
$menuItems['menu_items'][$row['id']]['ingredients'][$row['ingred_id']] = $row['ingred_id'];
$menuItems['error'] .= '';
}
if( $menuItems != null ) {
echo json_encode($menuItems);
//print_r($menuItems);
}
else {
echo json_encode(array('error' => 'There are currently no menu items for this location'));
}
}
}