我使用cakephp 2.0并且我提交的数据我要清理,数组结构如下我如何删除其中quantity = 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);
}
}
Run Code Online (Sandbox Code Playgroud)
被称为数组的结构($ 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] =>
)
)
)
Run Code Online (Sandbox Code Playgroud)
您可以使用数组键删除它:
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]);
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,这将在数组中创建间隙(不存在的索引),通常这不会是一个问题,但如果是,您可以使用重新索引数组array_values().