首页 > 解决方案 > 使用“list”函数时的PHP未定义偏移量消息

问题描述

今天早上,当我点击广告后,我的网站上出现了这个错误

我试过查看代码,但似乎没有发现任何问题

if (!function_exists('adforest_extarct_link')) {

    function adforest_extarct_link($string) {
        $arr = explode('|', $string);
        list($url, $title, $target, $rel) = $arr; /* This is line 148 */
        $rel = urldecode(adforest_themeGetExplode($rel, ':', '1'));
        $url = urldecode(adforest_themeGetExplode($url, ':', '1'));
        $title = urldecode(adforest_themeGetExplode($title, ':', '1'));
        $target = urldecode(adforest_themeGetExplode($target, ':', '1'));
        return array("url" => $url, "title" => $title, "target" => $target, "rel" => $rel);
    }

这是错误消息

未定义的偏移量:第 148 行 /customers/7/6/1/corpersmarket.com/httpd.www/wp-content/themes/adforest/inc/theme_shortcodes/short_codes_functions.php 中的 3

它实际上有 3 行错误:

Notice: Undefined offset: 1 in /customers/7/6/1/corpersmarket.com/httpd.www/wp-content/themes/adforest/inc/theme_shortcodes/short_codes_functions.php on line 148 
Notice: Undefined offset: 2 in /customers/7/6/1/corpersmarket.com/httpd.www/wp-content/themes/adforest/inc/theme_shortcodes/short_codes_functions.php on line 148 
Notice: Undefined offset: 3 in /customers/7/6/1/corpersmarket.com/httpd.www/wp-content/themes/adforest/inc/theme_shortcodes/short_codes_functions.php on line 148

标签: phpundefinedoffset

解决方案


问题大致是来自 list() 的 PHP 未定义偏移量的重复

然而,

list预计至少有 4 个参数 - 但您的$arr数组只有 1 个。所以以下三个是空的。(记住数组从 0 开始)。因此,您$string不包含使功能按预期工作的|字符。explode

解决方法

原来的:

    $arr = explode('|', $string);
    list($url, $title, $target, $rel) = $arr; /* This is line 148 */

变成:

    $arr = array_pad(explode('|', $string), 4, null);
    list($url, $title, $target, $rel) = $arr;

这是做什么的:

填充数组以包含至少 4 个值;这样这些list值将始终被填充,即使它们可能仍然为空。


推荐阅读