Reputation: 113
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?
Upvotes: 9
Views: 23311
Reputation: 663
$clear = array();
foreach($your_array_variable as $key=>$val){
$val = '';
$clear [$key] = $val;
}
print_r($clear);
The below code is to unset same array,
foreach($your_array_variable as $key=>$val){
$val = '';
$your_array_variable[$key] = $val;
}
print_r($your_array_variable);
Both of the above code will help you to just unset the values only and won't clear the keys. So keys will be as it is but values will be cleared.
Where it's output will be like below,
array(
[0]=>
[1]=>
)
if you use $your_array_variable = array();
then you will be getting the below output,
Array(
)
Upvotes: 2