PHP 使用 "list" 函数时未定义的偏移消息

PHP Undefined offset Message when using "list" function

我今天早上在我的网站上点击 post-ad

时出现此错误

我已经尝试查看代码,但似乎没有发现任何错误

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

这是错误消息

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

实际上有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

Question is broadly a duplicate of PHP undefined offset from list()

然而,

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

Workaround:

原文:

    $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;

这是做什么的:

Pads the array out 至少包含 4 个值;这样 list 值将始终被填充,即使它们可能仍然是空的。