【问题标题】:How to loop through an array of objects and get the values from a specific object?如何遍历对象数组并从特定对象中获取值?
【发布时间】:2020-09-02 11:31:48
【问题描述】:

基本上我已经设法转储了objects 中的array。我试图弄清楚如何从一组objects 中获取[options] 对象的值并将它们存储在一个数组中

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)
    }
  }
}

我尝试使用 foreach 循环遍历每个对象,但我偶然发现了一个错误。

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

我的目标是从[options] 对象中获取所有值,并将它们存储在一个数组中以供使用。

【问题讨论】:

  • "options" a) 不在数组的顶层,它在对象内的另一个数组内,并且 b) 该数组受到保护,因此您无法从外部访问它反正反对。也许该对象上定义了一个函数,可用于返回选项数据?

标签: php arrays loops object


【解决方案1】:

据我所见,您正在遍历 WC_Product_Attribute 对象数组,这是使用 WooCommerce 的标志,不是吗?

在这种情况下,有一个 getter 方法可用于从产品属性中获取选项:

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

更多in the docs

【讨论】:

  • 谢谢你,现在我只需要弄清楚如何循环并存储它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" }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-21
  • 1970-01-01
  • 1970-01-01
  • 2018-05-03
  • 2021-02-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多