我有这样的类别树: 我想用列来解析它到我的表:category_name,category_id和parent_id。
stdClass Object
(
[Name] => Shoes
[CategoryId] => 661
[GlobalIdentifier] =>
[Children] => stdClass Object
(
[Category] => Array
(
[0] => stdClass Object
(
[Name] => Female Shoes
[CategoryId] => 3
[GlobalIdentifier] =>
[Children] => stdClass Object
(
[Category] => Array
(
[0] => stdClass Object
(
[Name] => Shoes type 1
[CategoryId] => 2082
[GlobalIdentifier] =>
[Children] =>
)
[1] => stdClass Object
(
[Name] => Shoes type 2
[CategoryId] => 50
[GlobalIdentifier] =>
[Children] =>
)
[2] => stdClass Object
(
[Name] => Shoes type 3
[CategoryId] => 2098
[GlobalIdentifier] =>
[Children] =>
)
[3] => stdClass Object
(
[Name] => Shoes type 4
[CategoryId] => 7
[GlobalIdentifier] =>
[Children] => stdClass Object
(
[Category] => Array
(
[0] => stdClass Object
(
[Name] => Shoes type 5
[CategoryId] => 2797
[GlobalIdentifier] =>
[Children] =>
)
我正在尝试构建一个解析这棵树的函数。这就是我现在所拥有的,但它仍然不起作用。因为我可以直到树的最后一个节点,但不能回到它的父节点继续。我该如何解决这个问题?
$categoriesArr = $categories->Categories->Category[0];
function parseCategories($root, $parentId, $parentNode, $returnArr, $arrayNode = array(), $arrayIndex = -1){
var_dump('debug: '. var_export($returnArr, true));
if (is_array($root) == false && is_object($root)){
if (isset($root->Name)){
$returnArr[] = array('category_id' => $root->CategoryId, 'name' => $root->Name, 'parent_id' => $parentId);
print_r($root);
print_r("parentid ".$parentId);
if (isset($root->Children) && !empty($root->Children)) {
if (isset($root->Children->Category)) {
return parseCategories($root->Children->Category, $root->CategoryId, $root, $returnArr);
}
else {
if (count($arrayNode) > $arrayIndex+1){
print_r("count ".count($arrayNode));
print_r("arrayIndex ".$arrayIndex+1);
return parseCategories($arrayNode, $parentId, $parentNode, $returnArr, $arrayNode, $arrayIndex);
}
else {
print_r("abc");
}
}
}
else {
return $returnArr;
}
}
}