Instead of the loop, why not something like:
`module_load_include('inc', 'islandora', 'includes/utilities');
$namespace_sparql = implode('|', array_filter(array_map('islandora_get_namespace', $namespace_array), 'trim');`