Reputation: 2893
Hi I am having a few issues unsetting an entire row from a multidimensional array. I have an array that takes the following format
Array
(
[0] => Array
(
[ID] => 10000
[Date] => 21/11/2013
[Total] => 10
)
[1] => Array
(
[ID] => 10001
[Date] => 21/12/2013
[Total] => abc
)
...
)
I am looping this array to check that the Total contains only numbers or a period.
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;
}
}
}
}
So you can see from my array, the second element Total contains abc, therefore the whole element it is in should be removed. At the moment, with what I have, I am getting only that element removed
[1] => Array
(
[ID] => 10001
[Date] => 21/12/2013
)
How can I remove the whole element?
Thanks
Upvotes: 0
Views: 35
Reputation: 1063
Try this:
//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;
}
}
}
}
Upvotes: 1