我尝试使用 PHPunset(key)
函数根据嵌套数组中的值删除整个父数组。
嵌套数组看起来像
Array (
[0] => Array (
[ID] => 9909
[SHIPPING_AMOUNT] => 5
[TOTAL_TAX] => 0
[GRAND_AMOUNT] => 49.97
[ITEMS_AMOUNT] => 44.97
[ITEMS] => Array (
[0] => Array (
[CODE] => TEST
[QTY] => 1
[UNIT_PRICE] => 14.99 )
[1] => Array (
[CODE] => NNKIT
[QTY] => 1
[UNIT_PRICE] => 9.99 )
[2] => Array (
[CODE] => MAINKIT
[QTY] => 1
[UNIT_PRICE] => 19.99 )
)
)
[1] => Array (
[ID] => 9910
[SHIPPING_AMOUNT] => 5
[TOTAL_TAX] => 0
[GRAND_AMOUNT] => 74.96
[ITEMS_AMOUNT] => 69.96
[ITEMS] => Array (
[0] => Array (
[CODE] => NNKIT
[QTY] => 1
[UNIT_PRICE] => 9.99 )
[1] => Array (
[CODE] => MAINKIT
[QTY] => 3
[UNIT_PRICE] => 19.99 )
)
)
[2] => Array (
[ID] => 9911
[SHIPPING_AMOUNT] => 5
[TOTAL_TAX] => 0
[GRAND_AMOUNT] => 44.98
[ITEMS_AMOUNT] => 39.98
[ITEMS] => Array (
[0] => Array (
[CODE] => MAINKIT
[QTY] => 2
[UNIT_PRICE] => 19.99 )
)
)
[3] => Array (
[ID] => 9912
[SHIPPING_AMOUNT] => 5
[TOTAL_TAX] => 0
[GRAND_AMOUNT] => 29.98
[ITEMS_AMOUNT] => 24.98
[ITEMS] => Array (
[0] => Array (
[CODE] => TEST
[QTY] => 1
[UNIT_PRICE] => 14.99 )
[1] => Array (
[CODE] => NEWTEST
[QTY] => 1
[UNIT_PRICE] => 9.99 )
)
)
我检查的值是CODE = MAINKIT
. 如果嵌套数组中不存在,则应删除主数组(在本例中为 parent array[3]
)但以某种方式返回key
来自嵌套数组而不是父数组。
PHP代码:
foreach($array as $key => $value){
if(is_array($value) && $value['CODE'] != 'MAINKIT')
unset($key);
}