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;
    }
Member Avatar for LastMitch

@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!

Thanks LastMitch, Notices are gone :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.