-1

I have a site that worked fine and over the weekend my client cant access the maintenance report panel because this error appears

[Notice] Undefined offset: 0

The site works fine on dev so I know its not a php version issue / updating issue. Can anyone assist please? Heres the code it says the error is on line 44 which is return $parentNodes[0];

    public function getProblemPath(){
    $parentNodes = array_reverse($this->getParent($this->ParentNodeID, array()));
    return implode(' -> ', $parentNodes) . ' -> ' . $this->Title;
}

public function getProblemStart(){
    $parentNodes = array_reverse($this->getParent($this->ParentNodeID, array()));
    return $parentNodes[0];
}

public function getParent($ID, $nodes){
    if ($parent = DataObject::get_one("MaintenanceNode", array("ID" => $ID))){
        $nodes[] = $parent->Title;
        if ($parent->ParentNodeID){
            return $this->getParent($parent->ParentNodeID, $nodes);
        } else {
            return $nodes;
        }
    } else {
        return $nodes;
    }
}
tereško
  • 58,060
  • 25
  • 98
  • 150
L chowney
  • 43
  • 5

1 Answers1

-1

The notice is telling you that array index 0 does not exist. Before accessing an index you should check if it exists.

You can replace your return statement with the following:

return array_key_exists(0, $parentNodes) ? $parentNodes[0]: null;
Michał Szczech
  • 466
  • 4
  • 17