php – 删除foreach循环中的数组元素

栏目: PHP · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/8942885/deleting-an-array-element-within-foreach-loop

我有一个简单的数组,其中包含所有国家/地区的名称以及每个国家/地区在我的网站上注册的用户总数.它是这样的:

Array (
    [1] => Array ( [name] => Afghanistan [total] => 3 )
    [2] => Array ( [name] => Albania [total] => 0 )
)

而且,我正在尝试删除拥有0个用户的数组元素(国家/地区).

我已尝试使用此代码,但它无法正常工作:

foreach($country as $row) {
    if ($row['total'] == 0) {
        unset($row);
    }
}

这段代码有什么问题?

如果取消设置($row),则只删除局部变量.

而是获取密钥并删除它:

foreach ($country as $i => $row) {
    if ($row['total'] == 0) {
        unset($country[$i]);
    }
}

翻译自:https://stackoverflow.com/questions/8942885/deleting-an-array-element-within-foreach-loop


以上所述就是小编给大家介绍的《php – 删除foreach循环中的数组元素》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Beginning XSLT 2.0

Beginning XSLT 2.0

Jeni Tennison / Apress / 2005-07-22 / USD 49.99

This is an updated revision of Tennison's "Beginning XSLT", updated for the new revision of the XSLT standard. XSLT is a technology used to transform an XML document with one structure into another ......一起来看看 《Beginning XSLT 2.0》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

随机密码生成器
随机密码生成器

多种字符组合密码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具