【发布时间】: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) 该数组受到保护,因此您无法从外部访问它反正反对。也许该对象上定义了一个函数,可用于返回选项数据?