0

I have an array of recipes. Each recipe contains an array of ingredients:

[Waffle] => Array
        (
            [0] => stdClass Object
                (
                    [amount] =>  1
                    [measurement] => large
                    [ingredient] => egg 
                    [grocery] => dairy
                )

            [1] => stdClass Object
                (
                    [amount] =>  1
                    [measurement] => cup
                    [ingredient] => milk 
                    [grocery] => dairy
                )

        )

[Pancake] => Array
        (

            [0] => stdClass Object
                    (
                        [amount] =>  1
                        [measurement] => large
                        [ingredient] => egg
                        [grocery] => dairy
                    )

            [1] => stdClass Object
                (
                    [amount] =>  1
                    [measurement] => tablespoon
                    [ingredient] => maple syrup
                    [grocery] => pantry
                )

        )

I want to create a grocery list of all ingredients, compiling the amounts by ingredient and measurement, and ordered according to the grocery aisle.

So, given the above, it would output something like:

DAIRY

2 large eggs
1 cup milk

PANTRY

1 tablespoon maple syrup

Any help you can give writing a function to do this would be greatly appreciated.

3
  • 2
    Did you try anything already? or do you expect someone to do it all for you? Commented May 31, 2012 at 20:41
  • 3
    I think they'd look at me funny in the store if I asked for 2 large eggs, a cup of milk, and a tablespoon of maple syrup. ;) What have you tried so far? Commented May 31, 2012 at 20:41
  • I was hoping someone had done something like this before and had a suggestion for how to approach it since it is pretty complicated. I thought of trying to merge the arrays, but I run into problems because I need to get the sum of the amounts. I'm also running into issues where "egg" is treated as a separate ingredient than "eggs." I don't expect someone to write the whole solution for me, but some ideas on how to do this efficiently are appreciated. Commented Jun 1, 2012 at 6:18

1 Answer 1

0

Because the names of ingredients and scale of measurements varied so greatly, I ended up just grouping ingredients by grocery aisle and not combining the measurements.

I looped through arrays to build additional array sets containing the necessary data I needed to output the groups as needed.

I could have gone further and created a conversion table, then converted each measurement to standard scale, and then added the total of ingredients, but, fortunately, this isn't necessary at the moment.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.