如何PHP取消设置数组值

时间:2016-07-28 12:09:33

标签: php arrays

我想从数组中取消一些值。数组如下所示:

$data['total'] = Array ( 
 [0] => Array ( [title] => Sub-Total [code] => sub_total [text] => Rs. 1,222 ) 
 [1] => Array ( [title] => Free Shipping [code] => shipping [text] => Rs. 0 ) 
 [2] => Array ( [title] => Free Shipping [code] => [text] => Rs. 0 ) 
 [3] => Array ( [title] => VAT (1%) [code] => tax [text] => Rs. 12 ) 
 [4] => Array ( [title] => Total [code] => total [text] => Rs. 1,222 ) 
) 

我成功完成了unset($data['total'][1])unset($data['total'][2])

但是在某些时候阵列改变如下

$data['total'] = Array ( 
 [0] => Array ( [title] => Sub-Total [code] => sub_total [text] => Rs. 1,222 ) 
 [1] => Array ( [title] => Free Shipping [code] => shipping [text] => Rs. 0 ) 
 [2] => Array ( [title] => Free Shipping [code] => [text] => Rs. 0 ) 
 [3] => Array ( [title] => Total [code] => total [text] => Rs. 1,222 ) 
) 

因此,从数组中取消了错误的值。

  

如果数组中存在,我想以code 送货取消设置值。

我也尝试了如下

在Rizier评论后发现:[不工作]

foreach($data['totals'] as $key => $sub_array) {
    if($sub_array['code'] == 'tax') {
        unset($array[$key]);
    }
if($sub_array['code'] == 'shipping') {
        unset($array[$key]);
    }
}

请协助

0 个答案:

没有答案