Add if-not-empty check before foreach to prevent PHP warning (#4427)

Fix described in issue 4427 http://issues.joomla.org/tracker/joomla-cms/4427