我的ProductClass对象中有一个数组(子类别数组):
class ProductCategory{
public $name;
public $id;
public $subcategory = array();
public function __construct($categoryName, $categoryId, $subcategoryname, $subcategoryid){
$this->name = $categoryName;
$this->id = $categoryId;
$this->subcategory = array (
"subcategoryname" => $subcategoryname,
"subcategoryid" => $subcategoryid
);
}
}
我有一个看起来像这样的对象:
[0] => ProductCategory Object
(
[name] => Flower
[id] => 1
[subcategory] => Array
(
[subcategoryname] => Buds
[subcategoryid] => 8
)
)
[1] => ProductCategory Object
(
[name] => Concentrate
[id] => 2
[subcategory] => Array
(
[subcategoryname] => Live Resin
[subcategoryid] => 15
)
)
[2] => ProductCategory Object
(
[name] => Concentrate
[id] => 2
[subcategory] => Array
(
[subcategoryname] => Shatter
[subcategoryid] => 5
)
)
在我的一生中,我无法弄清楚如何访问子类别名称和子类别ID。
我可以轻松访问名称和ID,例如:$category->name
和$category->id
。
我这样使用print_r
:print_r($category->subcategory)
,我得到的输出看起来像这样:
Array ( [subcategoryname] => Buds [subcategoryid] => 8 )
所以我知道它在那里。我使用Array
时得到echo $category->subcategory
。
但是,在获取子类别名称和子类别ID值时,我尝试执行的操作似乎没有任何作用。
我尝试过:
$category->subcategory->subcategoryname
也:
$category->subcategory[0]
也:
$category->subcategory['subcategoryname']
也:
$category->subcategory['subcategory']['subcategoryname']
我似乎什么都没返回名字(或id)。