某天突然需要移除阵列(数组)的空白元素,发现以下代码居然没作用:
<?php
foreach($linksArray as $link) {
if($link == '') {
unset($link);
}
}
?>
Stack Overflow:
I thought it was worth mentioning that the code above does not work because unset(...) operates on the variable created by the foreach loop, not the original array that obviously stays as it was before the loop.
大意是:foreach内的变量为拷贝副本,unset无作用于原本的阵列(数组)。
后来Google发现以下函数:
array array_filter ( array
$array
[, callable$callback
[, int$flag
= 0 ]] )