0

Basically I have an array of objects that I've managed to dump. I'm trying to figure out how I can get the values from the [options] object from a set objects and store them in an array

array(4) {
  ["size"]=>
  object(WC_Product_Attribute)#2724 (1) {
    ["data":protected]=>
    array(6) {
      ["id"]=>
      int(0)
      ["name"]=>
      string(4) "Size"
      ["options"]=>
      array(3) {
        [0]=>
        string(6) "400 ml"
        [1]=>
        string(2) "1L"
        [2]=>
        string(2) "2L"
      }
      ["position"]=>
      int(0)
      ["visible"]=>
      bool(true)
      ["variation"]=>
      bool(false)
    }
  }
  ["fruit"]=>
  object(WC_Product_Attribute)#2723 (1) {
    ["data":protected]=>
    array(6) {
      ["id"]=>
      int(0)
      ["name"]=>
      string(5) "Fruit"
      ["options"]=>
      array(8) {
        [0]=>
        string(17) "100% Orange Juice"
        [1]=>
        string(5) "Apple"
        [2]=>
        string(4) "Pear"
        [3]=>
        string(9) "Pineapple"
        [4]=>
        string(12) "Passionfruit"
        [5]=>
        string(15) "Red Dragonfruit"
        [6]=>
        string(13) "Baobab Powder"
        [7]=>
        string(17) "Grapeseed Extract"
      }
      ["position"]=>
      int(1)
      ["visible"]=>
      bool(true)
      ["variation"]=>
      bool(false)
    }
  }
  ["good-source-of-vit-c"]=>
  object(WC_Product_Attribute)#2722 (1) {
    ["data":protected]=>
    array(6) {
      ["id"]=>
      int(0)
      ["name"]=>
      string(20) "Good Source of Vit C"
      ["options"]=>
      array(1) {
        [0]=>
        string(3) "Yes"
      }
      ["position"]=>
      int(2)
      ["visible"]=>
      bool(true)
      ["variation"]=>
      bool(false)
    }
  }
  ["fiber"]=>
  object(WC_Product_Attribute)#2721 (1) {
    ["data":protected]=>
    array(6) {
      ["id"]=>
      int(0)
      ["name"]=>
      string(5) "Fiber"
      ["options"]=>
      array(1) {
        [0]=>
        string(3) "Yes"
      }
      ["position"]=>
      int(3)
      ["visible"]=>
      bool(true)
      ["variation"]=>
      bool(false)
    }
  }
}

I've tried doing a foreach to loop through each object but I've stumbled upon an error.

foreach ($productsArray as $i => $value) {
    echo $value->options;
}

My objective is to get all the values from the [options] object store them in an array to be used.

1
  • 1
    "options" a) isn't at the top level of the array, it's inside another array inside an object, and b) that array is protected, so you can't access it from outside the object anyway. Perhaps the object has a function defined on it which can be used to return the options data? Commented Sep 2, 2020 at 11:34

1 Answer 1

2

From what I can see you are iterating over an array of WC_Product_Attribute objects and that's a sign of WooCommerce usage, isn't it?

In that case there is a getter method available to get options from a Product Attribute:

foreach ($productsArray as $i => $value) {
    var_dump( $value->get_options() );
}

More in the docs

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that, now I just need to figure out how I can loop through of these and store it array(3) { [0]=> string(6) "400 ml" [1]=> string(2) "1L" [2]=> string(2) "2L" } array(8) { [0]=> string(17) "100% Orange Juice" [1]=> string(5) "Apple" [2]=> string(4) "Pear" [3]=> string(9) "Pineapple" [4]=> string(12) "Passionfruit" [5]=> string(15) "Red Dragonfruit" [6]=> string(13) "Baobab Powder" [7]=> string(17) "Grapeseed Extract" } array(1) { [0]=> string(3) "Yes" } array(1) { [0]=> string(3) "Yes" }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.