简体   繁体   English

如何在 PHP 中取消整个数组的设置?

[英]How to unset entire array in PHP?

I have a loop that runs 47 times on my page.我有一个循环在我的页面上运行了 47 次。 During the course of each loop, any error messages are entered into err[] and are printed out.在每个循环过程中,任何错误消息都会输入 err[] 并打印出来。 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.每次迭代可能有 4 或 5 条错误消息,有时没有。 Is there an easier way of resetting the entire array after each iteration beyond running another foreach loop and unsetting each value?除了运行另一个 foreach 循环并取消设置每个值之外,是否有更简单的方法可以在每次迭代后重置整个数组? A way of clearing all contents and resetting the indexes without actually removing the array itself?一种清除所有内容并重置索引而不实际删除数组本身的方法?

你应该使用: unset ( $err );

将它设置为array() ,你应该没问题。

$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();如果你使用$your_array_variable = array(); then you will be getting the below output,然后你会得到以下输出,

Array(
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM