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 trialthimoschroeder
23,470 PointsAsort clarification needed !
Hey,
it's hard to grasp what's happening when we use the following line of code:
$sort = $item["title"];
$output[$id] = $sort;
Can anybody clarify this for understanding purposes?
Any help is highly appreciated !
Code function:
foreach ($catalog as $id => $item) {
if (strtolower($category) == strtolower($item["category"])) {
$sort = $item["title"];
$output[$id] = $sort;
}
}
asort($output);
return array_keys($output);
}
1 Answer
thimoschroeder
23,470 Points@ Ashish mehra - Thanks for your efforts, I got clear now, however I'm still struggling to understand when to use this:
$output[$id] = $sort;
Ashish Mehra
Courses Plus Student 340 PointsWe'have data like this in data.php file
$catalog[101] = [
"title" => "A Design Patterns: Elements of Reusable Object-Oriented Software",
"img" => "img/media/design_patterns.jpg",
"genre" => "Tech",
"format" => "Paperback",
"year" => 1994,
"category" => "Books",
"authors" => [
"Erich Gamma",
"Richard Helm",
"Ralph Johnson",
"John Vlissides"
],
"publisher" => "Prentice Hall",
"isbn" => '978-0201633610'
];
$catalog[102] = [
"title" => "Clean Code: A Handbook of Agile Software Craftsmanship",
"img" => "img/media/clean_code.jpg",
"genre" => "Tech",
"format" => "Ebook",
"year" => 2008,
"category" => "Books",
"authors" => [
"Robert C. Martin"
],
"publisher" => "Prentice Hall",
"isbn" => '978-0132350884'
];
In function.php we've some code like this
function catalog_sel($catalog,$category){
$output = [];
foreach ($catalog as $key => $value) {
if($category==null OR strtolower($category)==strtolower($value["category"])){
$sort = $value["title"];
$output[$key] = $sort;
}
}
asort($output);
return array_keys($output);
}
now here in function.php we create an empty array $output and below in statement we define $output[$key] = $sort; which assigned data to an empty array like this $output[102] = "Clean Code: A Handbook of Agile Software Craftsmanship" , $output[101] = "A Design Patterns: Elements of Reusable Object-Oriented Software" (in sorted order) but we don't want to return title to our function back we want to return the keys of function that's why we create an $output as an associative array so that we can hold the keys of particular title in the order that sorted.
Ashish Mehra
Courses Plus Student 340 PointsAshish Mehra
Courses Plus Student 340 PointsHey thimoschroeder,
I'll try to explain you through comments may it will help you
hope it help you