This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Now that you know how to grab specific items, the next step is to be able to sort those items to make it easier to check for a specific item.
Links
Steps Used for Sorting the Items
Step 1: Create an Associative Array for Sorting
Instead of $output being a simple indexed array, we will change this to an associative array with the key being the ID and the value being the item Title.
$sort = $item["title"];
$output[$id] = $sort;
Step 2: Sort the Array by Value and Return Only the Keys
asort($output);
return array_keys($output);
Step 3: Trim "The ", "A ", "An " from the beginning of the title
$sort = ltrim($sort,"The ");
$sort = ltrim($sort,"A ");
$sort = ltrim($sort,"An ");
Step 4: Sort the Full Catalog
To do this we will need to remove the early return from the function which was bypassing the sort. Instead, we will combine the two category conditionals. Either $category should be null (in this case we use every item) OR $category should match the item category.
if ($category == null OR strtolower($category) == strtolower($item["category"])) {
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up