Search code examples
phpmultidimensional-arrayunset

unset array using a variable in PHP


This is a sample of my array:

 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] => 
                            )

                    )

I want to delete/unset "productPrice", "sku" and "price" from the whole array. I have used this so far:

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

But the array changes and has thousands of entries so coding the unset like this would not be easy. I am new to PHP and have searched all I can and looked up the array functions for something suitable with no luck.


Solution

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