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.

adamp
  • 31
  • 1
  • 6

1 Answers1

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

foreach ($alldata['variants'] as $key => $value) {
    unset (
        $alldata['variants'][$key]['price'], 
        $alldata['variants'][$key]['sku']
    );
}
marekpw
  • 662
  • 2
  • 5
  • 19
  • 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? – adamp May 17 '17 at 19:22
  • Can you please elaborate? I'm not sure what you mean. – marekpw May 17 '17 at 20:34
  • I was just being an idiot, it is now working thanks :) – adamp May 18 '17 at 07:09