将Array与相同值组合,但将另一个数组字典添加到组合数组中

时间:2014-03-10 18:31:36

标签: 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'],
            '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] => 
)

如何创建所需的输出数组?我很困惑,我不知道在哪里!

1 个答案:

答案 0 :(得分:1)

使用关联数组。当您控制索引时,生活变得更加容易。在处理一对多数据库结果时尤其如此。

例如(我显然对您的模型做出假设/猜测):

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'));
        }
    }
}

在上面的示例中,您将创建一个菜单列表,其中menu.id是数组键。每个菜单都有一个值'成分',它同样是一个数组。在我的例子中,我使用ingred_id作为键和值。显然你会修改代码,使用ingred_id作为键,加上一组其他成分信息作为值。

请注意,根据数据库的大小,运行多个查询可能会更有效。就像现在一样,你为每一行提取了大量的冗余数据。