1

I have function like this:

function getTree($r, $html) {
            foreach ($r as $entry) {
                if (count($entry['children']) == 0) {
                        $html .= '<li>' . $entry['parent_entry_id'] . '</li>';
                    } else {
                        getTree($entry['children'], $html);
                    }
                    //var_dump($html);
                }
                return $html;
            }

If I call it like this

$html = '';
$dzoni = getTree($results, $html);
echo $dzoni;

E expect to get few list elements But I get empty string. It is not problem with the data. If I var_dump them I get the result. But result does not concatenate all the time. It simply stops at some point. var_dump example:

C:\wamp64\www\co_3\regular_view.php:72:string '<li>17</li>' 
C:\wamp64\www\co_3\regular_view.php:72:string '<li>17</li><li>18</li>' 
C:\wamp64\www\co_3\regular_view.php:72:string '<li>22</li>' 

What am I doing wrong?

1 Answer 1

3

When you recurse, you need to use the return value instead of discarding it:

getTree($entry['children'], $html);

Should be something like:

$html = getTree($entry['children'], $html);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.