在foreach循环中设置多维数组的值

时间:2011-07-01 20:29:51

标签: php arrays multidimensional-array unset

我得到了一个多维数组的对象,在某些情况下需要“过滤”。这是我写的功能:

foreach($data["index"] as $key => $value){
    if(preg_match("/EXPRESSION/",$value->property)){
        unset($data["index"][$key]);
    }
}

它不会返回任何错误,但是当数组是var_dumped时,我认为我未设置的值仍然存在。

我在另一个主题上发现了这个:

foreach ($this->result['list'] as $key => &$row) {
    if ($this_row_is_boring) {
        unset($this->result['list'][$key]);
    }
}

我认为与我写的唯一区别在于,当我调用$ data时,他会调用$ this->结果。

有什么想法吗?感谢

1 个答案:

答案 0 :(得分:0)

如果正则表达式不匹配,则永远不会调用unset()

您可以插入一个语句来确定preg_match()是否会返回true吗?

if(preg_match("/EXPRESSION/",$value->property)){
    unset($data["index"][$key]);
    echo "Match found with \$key: $key\n";
}
相关问题