This is nice. But, consider if you have 100 or more categories, how many times will you execute the query inside your function per request. You have already fetched all categories from a table. So, it would be much faster to pass an array of all categories as the second parameter and loop throughout them.