I have a loop that runs 47 times on my page. During the course of each loop, any error messages are entered into err[] and are printed out. I'm trying to blank the array after each iteration and I'm running into some trouble.
There could be 4 or 5 error messages per iteration, sometimes none. Is there an easier way of resetting the entire array after each iteration beyond running another foreach loop and unsetting each value? A way of clearing all contents and resetting the indexes without actually removing the array itself?
Use unset() function to remove array elements in a foreach loop. The unset() function is an inbuilt function in PHP which is used to unset a specified variable.
basically we will unset multiple keys from php array. if you see in php documentation there are not available directly remove multiple keys from php array. But we will create our own php custom function and remove all keys by given array value. In this example i created custom function as array_except().
unset() destroys the specified variables. The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy. If a globalized variable is unset() inside of a function, only the local variable is destroyed.
The reset() function moves the internal pointer to the first element of the array. Related methods: current() - returns the value of the current element in an array. end() - moves the internal pointer to, and outputs, the last element in the array.
You ought to use:
unset ( $err );
Set it to array()
, and you should be fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With