Okay вот моя рекурсия алго:Возникли проблемы с рекурсией алгоритмом
public function getCategoryTree($tree,$return = array()) {
foreach ($tree->children as $child) {
if (count($child->children) > 0)
$return[$tree->name] = $this->getCategoryTree($child, $return);
else
$return[] = $child->name;
}
return $return;
}
Вот фрагмент структуры данных Я пытаюсь пройти
Object(stdClass)#290 (6) {
["category_id"]=>
int(1)
["parent_id"]=>
int(0)
["name"]=>
string(4) "Root"
["position"]=>
int(0)
["level"]=>
int(0)
["children"]=>
array(2) {
[0]=>
object(stdClass)#571 (7) {
["category_id"]=>
int(2)
["parent_id"]=>
int(1)
["name"]=>
string(18) "Root MySite.com"
["is_active"]=>
int(0)
["position"]=>
int(0)
["level"]=>
int(1)
["children"]=>
array(11) {
[0]=>
object(stdClass)#570 (7) {
["category_id"]=>
int(15)
["parent_id"]=>
int(2)
["name"]=>
string(9) "Widgets"
["is_active"]=>
int(1)
["position"]=>
int(68)
["level"]=>
int(2)
["children"]=>
array(19) {
[0]=>
object(stdClass)#566 (7) {
["category_id"]=>
int(24)
["parent_id"]=>
int(15)
["name"]=>
string(16) "Blue widgets"
["is_active"]=>
int(1)
["position"]=>
int(68)
["level"]=>
int(3)
["children"]=>
array(0) {
}
}
<snip....>
Я пытаюсь получить структура PHP данных как такой
categories = array("Root" =>
array("Root MySite.com" =>
array("Widgets" =>
// final element is NOT an array
array ("Blue Widgets", "Purple Widgets" ...)
)
)
)
Я не совсем, кажется, чтобы получить структуру данных, я ищу с помощью моего рекурсивный Algo. Любая помощь будет отличной.
В конце концов, мне нужно, чтобы разобрать его снова на веб-интерфейсе и отобразить его, но еще одна проблема, на другой день ...
Когда он стал "круто", чтобы использовать * Algo *? – Kermit
Когда-то за последние 2 года, когда стало популярным слово «эпос» – bonez
Мне непонятно «Я пытаюсь сохранить его в Mongo DB как таковой». Итак, вам нужен способ конвертировать ваш объект в строку ??? объекты могут быть сохранены в виде строк при их сериализации. В php есть много способов сделать это. Двумя наиболее значимыми являются: [json-encode] (http://php.net/manual/es/function.json-encode.php) и [serialize] (http://php.net/manual/es/function .serialize.php) –