Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJavier MARQUEZ
11,877 PointsCan you please verify if ,modifications, made to function array_category are a good idea? Seems more clear to me
<?php
function array_category($catalog, $category){
$output = array();
if ($category == null){
return array_keys($catalog);
return $output;
}
else {
$output = array();
foreach ($catalog as $id => $item){
if (strtolower($category) == strtolower($item["category"])){
$output[] = $id;
}
}
return $output;
}
}
2 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi Javier,
It's ok to put the loop in an else block if that makes more sense to you but it does add extra lines of code.
You don't have to initialize the $output variable twice. It's enough to either do it at the top or in the else block.
Also, it's not useful to have 2 return statements in a row. The second one is unreachable. So you can remove return $output
that you have in your if
block.
Javier MARQUEZ
11,877 PointsThanks a lot, its more clear now.
Javier MARQUEZ
11,877 PointsJavier MARQUEZ
11,877 PointsI dont seem to be able to paste my code. What an embarrassment lol. I thought I was a bit more computer literate. Sorry.