Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Enhancing a Simple PHP Application Refactoring the Codebase Separating Concerns: MVC

Dan Neumann
PLUS
Dan Neumann
Courses Plus Student 10,318 Points

Access elements from a returned array in php

I am trying to answer this question:

Since the flavors are now ordered by the number of likes, it would be nice to display the number of likes next to each flavor. Instead of showing just the flavor name ("Cookie Dough"), change it to show the flavor name, followed by a space, followed by a plus sign, followed by the number of likes ("Cookie Dough +976"). (Look through flavors.php to see the structure of the flavors array and to figure out how to access the number of likes.)

but can't figure it out.

index.php
<?php

// INCLUDE MODEL FILE
require_once("flavors.php");



// CONTROLLER CODE
$flavors = get_flavors_by_likes(3);



// VIEW CODE
?><html>
<body>

    <h1>Ye Olde Ice Cream Shoppe</h1>

    <p>We sell amazing flavors of ice cream.</p>

    <h2>Most Popular Flavors</h2>

    <ul>
    <?php
        foreach ($flavors as $flavor) {
            echo "<li>" . $flavor[0] . " +" . $flavor[2] . "</li>";
        }
    ?>
    </ul>

</body>
</html>
flavors.php
<?php

// This function returns all the flavors
// ordered by the date they have been added.
function get_all_flavors() {

    $flavors = array(
        array("name" => "Vanilla", "likes" => 312),
        array("name" => "Cookie Dough", "likes" => 976),
        array("name" => "Peppermint", "likes" => 12),        
        array("name" => "Cake Batter", "likes" => 598),
        array("name" => "Avocado Chocolate", "likes" => 6),        
        array("name" => "Jalapeno So Spicy", "likes" => 3),        
    );

    return $flavors;

}

// This function receives a number as an argument.
// It returns the newest flavors, the most recent
// flavor first. (The number of flavors returned
// corresponds to the number in the argument.)
function get_recent_flavors($number) {

    $recent = array();
    $all = get_all_flavors();

    $total_flavors = count($all);
    $position = 0;

    foreach($all as $flavor) {
        $position = $position + 1;
        if ($total_flavors - $position < $number) {
            $recent[] = $flavor;
        }
    }

    return array_reverse($recent);

}

// This function receives a number as an argument.
// It returns the most popular flavors based on
// the number of likes, the most popular flavor
// first. (The number of flavors returned corresponds
// to the number in the argument.)
function get_flavors_by_likes($number) {

    $all = get_all_flavors();

    $total_flavors = count($all);
    $position = 0;

    $popular = $all;
    usort($popular, function($a, $b) {
        return $b['likes'] - $a['likes'];
    });

    return array_slice($popular, 0, $number);

}

?>

1 Answer

Hi Dan,

Great question. Since each $flavor holds an associative array, we can access the values by passing the name of the key (or property) into brackets.

Edit: I incorrectly suggest the term 'property' above. Properties represent the variables of an object. However, when dealing with arrays, the correct term is 'key'. Reference: PHP: Arrays.

<?php

$flavor =  array("name" => "Vanilla", "likes" => 312);

echo $flavor["name"];  // Vanilla
echo $flavor["likes"]; // 312

?>

I'll be happy to provide more explanation if needed. I'm deliberately holding back to help you discover how to piece the solution together.

Hope this helps,

Cheers

Dan Neumann
Dan Neumann
Courses Plus Student 10,318 Points

Thanks Robert. I did this:

    <?php
        foreach ($flavors as $flavor) {
            echo "<li>" . $flavor["name"] . " +" . $flavor["likes"] . "</li>";
        }
    ?>

and that worked. Thanks again.

Excellent! That's exactly what I wrote for the challenge.