0

So I've been given this by a tutor of mine, to complete I'm slightly stuck. Ive tired loads of methods but not get the right way of displaying the data.

Instructions:

/*
* Below is an array of cakes and information about them.
* Please write a script that generates the following html snippet
*
*    <ul>
*      <li>Wedding Cake<br />Flavour: hopes and dreams</li>
*       <li>Chocolate Gateau<br />Flavour: chocolate</li>
*       <li>Black Forest Gateau<br />Flavour: fruity</li>
*       <li>Victoria Sponge<br />Flavour: vanilla</li>
*       <li>Tottenham Cake<br />Flavour: strawberry</li>
*    </ul>
*
* (html or xhtml <br> / <br /> is fine!)
* Note that the list is ordered in descending order of price.
*/

Input data:

$cakes = array(
    "chocolate_gateau" => array(
        "flavour" => "chocolate",
        "price" => 3.50
    ),
    "victoria_sponge" => array(
        "flavour" => "vanilla",
        "price" => 1.50
    ),
    "black_forest_gateau" => array(
        "flavour" => "fruity",
        "price" => 2.20
    ),
    "tottenham_cake" => array(
        "flavour" => "strawberry",
        "price" => 0.58
    ),
    "wedding_cake" => array(
        "flavour" => "hopes and dreams",
        "price" => 5.23
    )
);

My code:

function cakeList($cakes) {
    echo "<ul>";
        foreach($cakes as $value) {
            if (is_array($value))
                cakeList($value);
            else
                echo '<li>' . $value . '</li>';
            }
            echo "</ul>";
        }
        echo cakeList($cakes);

They want me to produce a list within html, to display the data within the comments. Any ideas or advice?

2
  • 1
    I don't really see any difficulties, but may be loads of methods is a little too specific. Could you try to explain even less what you have tried and where you are stuck ? Commented Jan 29, 2014 at 14:52
  • 1
    What is your expected output? Commented Jan 29, 2014 at 14:53

3 Answers 3

3

foreach($someArray as $key => $value)

will allow you to get the keys of an array as well as the value.

Also entries in an array can be grabbed using the following syntax:

$a = array("foo"=>"Bar");
echo "The value at foo is ".$a['foo'];

The attempt you have can be updated with the above to get the result you need (remember you already know that $value is an array in your example).

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

Comments

0

First, you need to sort the list on price, i recommend using http://php.net/usort for this. After you have sorted out the sorting, you can basically loop over the results to print them

function cakeList ($cakes)
{
    echo '<ul>';
    foreach ($cakes as $cake => $details)
    {
        echo '<li>' . $cake . '<br/>' . $details['flavour'] . '<li/>';
    }
    echo '</ul>';
}

Comments

0

Sort by price descending by passing array_column()'s return value as a parameter of array_multisort() or sort with uasort().

Loop over the array and print the desired data after sanitizing the cake name values.

Demo

array_multisort(array_column($cakes, 'price'), SORT_DESC, $cakes);
echo "<ul>\n";
foreach ($cakes as $name => $props) {
    printf(
        "\t<li>%s<br />Flavour: %s</li>\n",
        ucwords(str_replace('_', ' ', $name)),
        $props['flavour']
    );
}
echo "</ul>";

Output:

<ul>
    <li>Wedding Cake<br />Flavour: hopes and dreams</li>
    <li>Chocolate Gateau<br />Flavour: chocolate</li>
    <li>Black Forest Gateau<br />Flavour: fruity</li>
    <li>Victoria Sponge<br />Flavour: vanilla</li>
    <li>Tottenham Cake<br />Flavour: strawberry</li>
</ul>

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.