Optimize Foreach inside Foreach

Job ID: 32274523

Budget: $10 – $20 USD

Hello

I need to optimize this code:
https://www.toptal.com/developers/hastebin/eqewonozix.php-template

Explaining the code, $temporadas have about 20 rows, and $episodios have about 1000 rows.. I need to put each episodio with temporadaID inside current temporada container..

The problem as you can see, is that if we have 20 rows of $temporada and 1000 rows of $episodio, we will loop 20,000 times.

I though in leave $episodio foreach outside $temporada, and organize the episodios with Javascript after complete the loop.
Related categories: PHP JavaScript MySQL jQuery