I could spend five minutes and fix it, or...
So I had originally made a different array for each entity in battle. Each had the character id in its title and all the stats as named entries in the array. I keep needing to iterate through all the arrays in different ways and coming up with silly tricks to get around this. Now it just seems so obvious that I should just keep them all in a multidimensional array, at which point I could just do things like foreach $array[team] as $t or count $array[id] or whatever. This requires quite a significant shift in overall vision, and maybe this mixup is the impetus I need to burn it all down. Haha. Hahahaha! Heh.
Oh well, this was a learning experience from the start.