我使用cakephp 2.0并且我提交的数据我要清理,数组结构如下我如何删除数量= null的元素(quoteitem)?
我有这个,但它不起作用;
foreach($this->request->data['Quoteitem'] as $qi) {
if($qi['quantity']==null){
echo 'quantity is null,delete this quote item from array';
unset($qi);
}
}
名为($ this-> request-> data)
的数组结构Array
(
[Quote] => Array
(
[customer_id] => 72
[user_id] => 104
)
[Range] => Array
(
[id] =>
)
[Quoteitem] => Array
(
[0] => Array
(
[product_id] =>
[unitcost] =>
[quantity] => 1
)
[1] => Array
(
[product_id] =>
[unitcost] =>
[quantity] => 22
)
[2] => Array
(
[product_id] => 339
[unitcost] => 5
[quantity] =>
)
)
)
答案 0 :(得分:5)
您可以使用数组键删除它:
foreach($this->request->data['Quoteitem'] as $key => $qi) {
if($qi['quantity'] == null){
echo 'quantity is null,delete this quote item from array';
unset($this->request->data['Quoteitem'][$key]);
}
}
请注意,这会在数组中创建间隙(不存在的索引),通常这不会有问题,但如果是,您可以使用array_values()
重新索引数组。
答案 1 :(得分:1)
Foreach制作副本,试试这个:
foreach($this->request->data['Quoteitem'] as $key => $qi) {
if($qi['quantity']==null){
echo 'quantity is null,delete this quote item from array';
unset($this->request->data['Quoteitem'][$key]);
}
}