嗨,我遇到了一些问题,无法从多维数组中取消整行。我有一个采用以下格式的数组
Array
(
[0] => Array
(
[ID] => 10000
[Date] => 21/11/2013
[Total] => 10
)
[1] => Array
(
[ID] => 10001
[Date] => 21/12/2013
[Total] => abc
)
...
)
我正在循环访问此数组,以检查“总计”是否仅包含数字或句点。
foreach($this->csvData as &$item) {
foreach($item as $key => $value) {
if($key === 'Total') {
$res = preg_replace("/[^0-9.]/", "", $item[$key] );
if(strlen($res) == 0) {
unset($item[$key]);
} else {
$item[$key] = $res;
}
}
}
}
因此您可以从我的数组中看到第二个元素Total包含abc,因此应删除其中的整个元素。目前,我所拥有的只是删除该元素
[1] => Array
(
[ID] => 10001
[Date] => 21/12/2013
)
如何删除整个元素?
谢谢
答案 0 :(得分:2)
尝试一下:
//Add key for outer array (no longer need to pass-by-reference)
foreach($this->csvData as $dataKey => $item) {
foreach($item as $key => $value) {
if($key === 'Total') {
$res = preg_replace("/[^0-9.]/", "", $item[$key] );
if(strlen($res) == 0) {
// Unset the key for this item in the outer array
unset($this->csvData[$dataKey]);
} else {
$item[$key] = $res;
}
}
}
}