Regarding your comments, sorry not noticed before. And what do you want to get as a result of the algorithm?
I can offer two ways. The second is more interesting, but not every task can be applied. Try it.
1. There is no typing of variables in PHP, so it spends a lot of resources trying to figure out how to do it and do it))
You will experience a performance boost even in Ruby or Java, not to mention C ++ compiled types))
As for syntax similarity ... Well, there is a very close Perl - it also often happens on hosting ... But I'm not sure that you write a program on it right away - it’s still very different .. And there are also questions about speed .
I would still recommend implementing the main functionality on PHP, and implementing the comparison operations themselves in C ++ and calling them via the CGI interface with redirects ...
2. And the second point - try to build the data in this way (even if you have to repartition it for several, etc.), so that you can use the standard PHP functions
array_diff - they will work much faster than nested loops.