Joe Tuley
Joe Tuley

Reputation: 1

Print a 2d array as formatted HTML list items sorted by a column value

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?

Upvotes: 0

Views: 82

Answers (3)

mickmackusa
mickmackusa

Reputation: 47894

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>

Upvotes: 0

Pieter De Schepper
Pieter De Schepper

Reputation: 476

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

Upvotes: 0

Jim
Jim

Reputation: 22656

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).

Upvotes: 3

Related Questions