2011-12-06 52 views
1

我遇到了一個PHP錯誤,通過我所有的互聯網scrummaging - 我一直無法破解。PHP抵消錯誤

錯誤說:

Error message Notice: Undefined offset: 3 in include() (line 89 of /home/devced/public_html/site/sites/all/themes/cedncsu/page.tpl.php).

所以,看在我的代碼,我現在有(實際使用下面的註釋):

<?php 
    // Get Base URL 
    global $base_url; 

    // Get the Page's Parent Menu Item 
    $menuParent = menu_get_active_trail(); 

    // Since it returns an array, make sure to target what you are looking for 
    // You should print_r what menu_get_active_trail() to see what else it gives you 
    $menuParent = $menuParent[1]['link_title']; 
    $menuParent = strtolower(str_replace(" ", "-", $menuParent)); 
    $menuParent = preg_replace('/[^\w\d_ -]/si', '', $menuParent); 

    // Generate class specific for department page headers 
    $menuParentDepartment = menu_get_active_trail(); 
    $menuParentDepartment = $menuParentDepartment[3]['link_title']; // This is where they say the error is 
    $menuParentDepartment = strtolower(str_replace(" ", "-", $menuParentDepartment)); 
    $menuParentDepartment = preg_replace('/[^\w\d_ -]/si', '', $menuParentDepartment); 

    // Current Page space replace/lowercase 
    $currentTitle = strtolower(str_replace(" ", "-", $title)); 
    $currentTitle = preg_replace('/[^\w\d_ -]/si', '', $currentTitle); 
?> 

我覺得這是一個普遍的編碼做法我很想念,但是至今一直都很難過。任何幫助將不勝感激。謝謝!

回答

4

很簡單:$menuParentDepartment[3]沒有定義。在嘗試索引索引之前,您需要確保索引(或者稱爲PHP調用它,偏移量)存在。使用isset

+0

+1,@Zach你確定menu_get_active_trail()總是返回3個或3個以上的數組元素嗎? – GoodSp33d

+0

甜! Yup @ kantu,這非常有道理,「爲什麼」它會顯示錯誤 - 因爲情況並非總是如此,所以如果我檢查一個值是否在第三位,或者不是第一位,那麼運行其他部分將會在其他情況下無用。謝謝您的幫助! – Zach