我在foreach循环中通过引用访问数组,但是unset()函数似乎不起作用:
foreach ( $this->result['list'] as &$row ) {
if ($this_row_is_boring) {
unset($row);
}
}
print_r($this->result['list']); // Includes rows I thought I unset
有什么建议吗?谢谢!
foreach ($this->result['list'] as $key => &$row) {
if ($this_row_is_boring) {
unset($this->result['list'][$key]);
}
}
foreach ($this->result['list'] as $key => &$row) {
if ($this_row_is_boring) {
unset($this->result['list'][$key]);
}
}
unset($row);
请记住:如果您正在使用带有引用的 foreach
,则应使用 unset 解除引用,以便 foreach
不会将下一个覆盖在其上。 更多信息
unset($row)
后,变量$row
被取消设置。这并不意味着$row
中的数据被删除;列表还有一个指向$row
的元素。unset
会删除此标签,但不会影响实际数据。如果所有标签都被删除,则数据会自动删除。