How is this used in practice? I see that you're looping through the 'rows' in $thiscat. I think your $row variable needs to be a $rowS and do an inner loop to compare each one instead of the same one to all the $thiscat[$query_field_id] values.