使用PHP将对象转换为数组

时间:2016-12-14 21:13:45

标签: php arrays

我有一个从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类型转换为没有运气的数组。

请问有人可以说明我在哪里出错吗?非常感谢您提供的任何见解。

1 个答案:

答案 0 :(得分:0)

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

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

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