0

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.

0

1 Answer 1

0
unset ($alldata['productPrice']);

foreach ($alldata['variants'] as $key => $value) {
    unset (
        $alldata['variants'][$key]['price'], 
        $alldata['variants'][$key]['sku']
    );
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the help, I forgot to mention that i get the $alldata array from a foreach. Your code works but only outside of the foreach. Is there any reason it wouldnt work inside a foreach?
Can you please elaborate? I'm not sure what you mean.
I was just being an idiot, it is now working thanks :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.