Do you really need to keep a list of $key4 ? I think you could just count them, $key4 as both a key and a value looks at bit odd, and you only seem to be counting them by keeping an array of repeated values. So, you could get rid of the useless arrays and replace them with a simple integer.
#so replace this
BTW In the code you posted as the problem area the sorts are doing nothing useful, just taking up time, but maybe you're doing other stuff in there too.