Look at the log error log file in Apache. There are many reasons for crashes. Most of them are caused by your own code problems. In that case, you should optimize your code as much as possible. This will also happen if the website is attacked by hackers. For such problems, it is recommended to check Apache's error logs yourself and learn to analyze them yourself. If you really can't solve it, you can only restart Apache.
Your foreach loops an empty array, and then takes the key name. Of course, an error will be reported. Don’t give you an empty array
I have also encountered similar problems, but as a novice, it is difficult to find the cause. The easiest way is to reinstall Apache.
Look at the log error log file in Apache. There are many reasons for crashes. Most of them are caused by your own code problems. In that case, you should optimize your code as much as possible. This will also happen if the website is attacked by hackers. For such problems, it is recommended to check Apache's error logs yourself and learn to analyze them yourself. If you really can't solve it, you can only restart Apache.