Answer the question
In order to leave comments, you need to log in
Nested structure by lines, recursion
Tell me, please, how to solve the problem, recursion does not work:
There is a nesting tree:
- Овощи
- картошка
- топинамбур
- капуста
- молодая
- старая
- буряк
- свежий
- Фрукты
- бананы
- яблоки
- груши
- ягоды
- арбуз
- малина
- по дешевле
- по дороже
function getStructToLine($getStruct, $tree = true)
{
$result = array();
if($getStruct && is_array($getStruct))
{
foreach($getStruct as $line => $user)
{
if($tree)
$result[$line][] = $user['user_id'];
else
$result[$line] = $user['user_id'];
if(isset($user['items']))
{
$child = $this->getStructToLine($user['items'], false);
$result[$line][] = $child;
}
}
}
return $result;
}
Answer the question
In order to leave comments, you need to log in
Three questions:
1. what is the exact structure of $getStruct?
2. With what parameters is getStructToLine() initially called?
3. what error is produced?
Probably typos:
1. 'user_id' key in $user['user_id']
2. $this->getStructToLine(...)
If you haven't already, the advice is:
1. Your array of users is an object of class User with the properties
2. make the recursive function signature like this:
where Array is the storage that You need it as an answer and into which you need to gradually overtake the data from the first argument of the recursive function until it becomes "empty".
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question