0

I am trying to figure out why I am getting this error notice, Does anyone have an idea as to why this is
happening.

Notice: Undefined offset: 5

on line: if ($tree[$i]['level'] < $tree[$i+1]['level']) {
on line: } elseif ($tree[$i]['level'] == $tree[$i+1]['level']) {
on line: $diff = $tree[$i]['level'] - $tree[$i+1]['level'];

public function treeAsHtml() {
        $tree = $this->getTree();
        $html = "<ul>\n";
        for ($i=0; $i<count($tree); $i++) {
            $html .= "<li>" . $tree[$i][$this->name];
            if ($tree[$i]['level'] < $tree[$i+1]['level']) {
                $html .= "\n<ul>\n";
            } elseif ($tree[$i]['level'] == $tree[$i+1]['level']) {
                $html .= "</li>\n";
            } else {
                $diff = $tree[$i]['level'] - $tree[$i+1]['level'];
                $html .= str_repeat("</li>\n</ul>\n", $diff) . "</li>\n";
            }
        }
        $html .= "</ul>\n";
        return $html;
    }
2
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by mbhanley
0

@mbhanley

Notice: Undefined offset: 5

The issue is here:

$tree[$i+1]

You already establish this:

($i=0; $i<count($tree); $i++)

The $i++ is $i+1 so you don't need to repeat that much.

You need to rewrite this:

if ($tree[$i]['level'] < $tree[$i+1]['level']) {
} elseif ($tree[$i]['level'] == $tree[$i+1]['level']) {
$diff = $tree[$i]['level'] - $tree[$i+1]['level'];

So just take out the +1 and rewrite the whole if/elseif/else statement and you're all set!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.