清除 PHP 数组值的最佳方法 [英] Best way to clear a PHP array's values

查看:45
本文介绍了清除 PHP 数组值的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪个更有效地清除数组中的所有值?第一个需要我在第二个示例的循环中每次都使用该函数.

Which is more efficient for clearing all values in an array? The first one would require me to use that function each time in the loop of the second example.

foreach ($array as $i => $value) {
    unset($array[$i]);
}

或者这个

foreach($blah_blah as $blah) {
    $foo = array();
    //do something
    $foo = null;
}

推荐答案

就像 Zack 在下面的评论中所说的那样,您可以使用

Like Zack said in the comments below you are able to simply re-instantiate it using

$foo = array(); // $foo is still here

如果你想要更强大的东西,使用 unset 因为它也会从符号表中清除 $foo,如果你稍后需要数组,只需再次实例化它.

If you want something more powerful use unset since it also will clear $foo from the symbol table, if you need the array later on just instantiate it again.

unset($foo); // $foo is gone
$foo = array(); // $foo is here again

这篇关于清除 PHP 数组值的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆