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 trial

PHP Build a Basic PHP Website (2018) Listing and Sorting Inventory Items Sorting Array Items

thimoschroeder
thimoschroeder
23,470 Points

Asort 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);
}

Hey thimoschroeder,

I'll try to explain you through comments may it will help you

foreach ($catalog as $id => $item) {    //using foreach we're looping through catalog array 
                                                               //here id is a key i.e [101][201] & item is an value in form of array which contain data
        if (strtolower($category) == strtolower($item["category"])) {  
//if the category(getting through function)  match with item category we execute below code
            $sort = $item["title"];  //here we create new variable with name sort and assign it value of current [item] title  
            $output[$id] = $sort;            //here we're pussing the $sort variable along with key i.e. catalog[key]  
        }
    }
    asort($output);  //here we're sorting the $output on the basis of title 
    return array_keys($output); //now here we're returning all the keys i.e.[101],[102] in the sorted order
}     

hope it help you

1 Answer

thimoschroeder
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;

We'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.