【发布时间】:2017-04-30 08:31:47
【问题描述】:
如果我运行以下代码,我有一个从 Magento API 检索数据的脚本:
$category_tree = $client->catalogCategoryTree($session_id);
print_r($category_tree);
输出如下:
stdClass Object
(
[category_id] => 1
[parent_id] => 0
[name] => Root Catalog
[position] => 0
[level] => 0
[children] => Array
(
[0] => stdClass Object
(
[category_id] => 2
[parent_id] => 1
[name] => Root Category
[is_active] => 1
[position] => 1
[level] => 1
[children] => Array
(
[0] => stdClass Object
(
[category_id] => 8
[parent_id] => 2
[name] => Designer
[is_active] => 1
[position] => 1
[level] => 2
[children] => Array
(
[0] => stdClass Object
(
[category_id] => 12
[parent_id] => 8
[name] => Chanel
[is_active] => 1
[position] => 1
[level] => 3
[children] => Array
(
)
)
[1] => stdClass Object
(
[category_id] => 13
[parent_id] => 8
[name] => Chanel
[is_active] => 1
[position] => 2
[level] => 3
[children] => Array
(
)
)
[2] => stdClass Object
(
[category_id] => 14
[parent_id] => 8
[name] => Chanel
[is_active] => 1
[position] => 3
[level] => 3
[children] => Array
(
)
)
[3] => stdClass Object
(
[category_id] => 15
[parent_id] => 8
[name] => Chanel
[is_active] => 1
[position] => 4
[level] => 3
[children] => Array
(
)
)
[4] => stdClass Object
(
[category_id] => 16
[parent_id] => 8
[name] => Chanel
[is_active] => 1
[position] => 5
[level] => 3
[children] => Array
(
)
)
)
)
[1] => stdClass Object
(
[category_id] => 7
[parent_id] => 2
[name] => Shop Bags
[is_active] => 1
[position] => 2
[level] => 2
[children] => Array
(
)
)
[2] => stdClass Object
(
[category_id] => 5
[parent_id] => 2
[name] => DifferentCategory
[is_active] => 1
[position] => 3
[level] => 2
[children] => Array
(
[0] => stdClass Object
(
[category_id] => 6
[parent_id] => 5
[name] => 1
[is_active] => 1
[position] => 1
[level] => 3
[children] => Array
(
)
)
)
)
[3] => stdClass Object
(
[category_id] => 4
[parent_id] => 2
[name] => Sample Category
[is_active] => 1
[position] => 6
[level] => 2
[children] => Array
(
)
)
)
)
)
)
在存储name 值的同时尝试将其转换为数组时,我在执行print_r() 函数时收到以下错误:
Notice: Trying to get property of non-object in C:\xampp\htdocs\create_products.php on line 151
Notice: Trying to get property of non-object in C:\xampp\htdocs\create_products.php on line 151
Notice: Trying to get property of non-object in C:\xampp\htdocs\create_products.php on line 151
Notice: Trying to get property of non-object in C:\xampp\htdocs\create_products.php on line 151
Notice: Trying to get property of non-object in C:\xampp\htdocs\create_products.php on line 151
Notice: Trying to get property of non-object in C:\xampp\htdocs\create_products.php on line 151
Array ( [0] => [1] => [2] => [3] => [4] => [5] => )
我的代码如下:
$result = [];
foreach ($category_tree as $designer_category) {
$result[] = $designer_category->name;
}
print_r($result);
我需要能够将name 的值存储为一个数组。我试过将$category_tree 类型转换为一个没有运气的数组。
任何人都可以阐明我在哪里出错了吗?非常感谢您提供的任何见解。
【问题讨论】:
-
我不确定您想要什么行为,但目前您所做的只是循环访问顶级对象的属性。
-
树的顶层只有一个类别,所以循环它没有意义。只需使用
$category_tree->name。如果要获取所有名称,则需要递归处理对象,循环 $category_tree->children`。