Filtering by value X from array Y with a fallback
Edit:


  You code is over complex knowing there are some really good built in php functions to filter an array.


I think that is where the 'special hardcore dev' started disliking your code. Here an example that uses an anonymous function and array_filter. It also does the same as in the other answer. Loop over the $types instead of the tags. I think it is safe to say that there will be more tags then types. 

function filterTagsByType($tags, $types, $count = 1)
{
    $tagFilter = function($type) {
        return function($tag) use($type) {
            return $tag-&gt;type == $type;
        };
    };

    foreach ($types as $type) {
        $filteredTags = array_filter($tags, $tagFilter($type));
        if ( count($filteredTags) &gt; 0 ) {
            return $filteredTags;
        }
    }

    return array();
}


You should ofcourse still add the count check. this is easy with array_slice:

return array_slice($filteredTags, 0, $count);


Not sure how this is performance wise, but it shouldn't be an issue.