使用PHP中的变量取消设置数组

时间:2017-05-17 18:57:05

标签: php multidimensional-array unset

这是我的数组样本:

 Array

    (
        [productId] => 7740792
        [productCode] => 1019534
        [productPrice] => Array
            (
                [current] => Array
                    (
                        [value] => 150
                        [text] => £150.00
                    )

                [previous] => Array
                    (
                        [value] => 0
                        [text] => £0.00
                    )

                [rrp] => Array
                    (
                        [value] => 0
                        [text] => £0.00
                    )

                [xrp] => Array
                    (
                        [value] => 150
                        [text] => £150.00
                    )

                [currency] => GBP
                [isMarkedDown] => 
                [isOutletPrice] => 
            )

        [variants] => Array
            (
                [0] => Array
                    (
                        [variantId] => 7740915
                        [sku] => 5784194
                        [isInStock] => 1
                        [isLowInStock] => 1
                        [price] => Array
                            (
                                [current] => Array
                                    (
                                        [value] => 150
                                        [text] => £150.00
                                    )

                                [previous] => Array
                                    (
                                        [value] => 150
                                        [text] => £150.00
                                    )

                                [rrp] => Array
                                    (
                                        [value] => 0
                                        [text] => £0.00
                                    )

                                [xrp] => Array
                                    (
                                        [value] => 150
                                        [text] => £150.00
                                    )

                                [currency] => GBP
                                [isMarkedDown] => 
                                [isOutletPrice] => 
                            )

                    )

                [1] => Array
                    (
                        [variantId] => 7740906
                        [sku] => 5784195
                        [isInStock] => 1
                        [isLowInStock] => 
                        [price] => Array
                            (
                                [current] => Array
                                    (
                                        [value] => 150
                                        [text] => £150.00
                                    )

                                [previous] => Array
                                    (
                                        [value] => 150
                                        [text] => £150.00
                                    )

                                [rrp] => Array
                                    (
                                        [value] => 0
                                        [text] => £0.00
                                    )

                                [xrp] => Array
                                    (
                                        [value] => 150
                                        [text] => £150.00
                                    )

                                [currency] => GBP
                                [isMarkedDown] => 
                                [isOutletPrice] => 
                            )

                    )

我想从整个数组中删除/取消设置“productPrice”,“sku”和“price”。 到目前为止我用过这个:

unset($alldata[0]['variants'][0]['price']);
unset($alldata[0]['variants'][1]['price']);

但是数组会发生变化并且有数千个条目,因此编写未设置的内容并不容易。我是PHP的新手,我已尽力搜索并查找数组函数以获得适合的运气。

1 个答案:

答案 0 :(得分:0)

unset ($alldata['productPrice']);

foreach ($alldata['variants'] as $key => $value) {
    unset (
        $alldata['variants'][$key]['price'], 
        $alldata['variants'][$key]['sku']
    );
}