【问题标题】:Converting an Object to an Array with PHP使用 PHP 将对象转换为数组
【发布时间】: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`。

标签: php arrays


【解决方案1】:

它不起作用,因为你有对象,而不是数组。 你必须对所有的孩子越来越深。

尝试使用递归,它不是理想的例子,但显示了想法:

function goDeeper($object, &$results) {
    if (!empty($object->name)) {
        $results[] = $object->name;
    }

    if (!empty($object->children)) {
        foreach ($object->children as $child) {
            goDeeper($child, $results);
        }
    }
}

$results = [];
$category_tree = $client->catalogCategoryTree($session_id);
goDeeper($category_tree, $results);

print_r($results);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-24
    • 2022-01-20
    • 2022-01-14
    • 2011-05-30
    • 2019-09-18
    • 2011-11-08
    • 2014-11-09
    • 1970-01-01
    相关资源
    最近更新 更多