【问题标题】:PHP Offset ErrorPHP 偏移量错误
【发布时间】:2011-12-06 21:46:19
【问题描述】:

我遇到了一个 PHP 错误,通过我在 Internet 上的所有搜索,我一直无法破解。

错误提示:

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);
?>

我认为这是我所缺少的一般编码实践,但迄今为止一直被难住了。任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: php drupal offset


    【解决方案1】:

    很简单:$menuParentDepartment[3] 没有定义。在尝试索引它之前,您需要确保索引(或 PHP 称之为偏移量)存在。使用isset

    【讨论】:

    • +1 ,@Zach 你确定 menu_get_active_trail() 总是返回 3 个或超过 3 个数组元素吗?
    • 甜蜜!是的@kantu,这完全有道理“为什么”它会显示错误 - 因为情况并非总是如此,所以如果我首先检查一个值是否在第三个位置,然后运行这些其他部分会在其他情况下无用。感谢您的帮助!
    猜你喜欢
    • 2011-08-03
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    相关资源
    最近更新 更多