PHP警告-非法字符串偏移量 [英] PHP Warning - Illegal string offset

查看:57
本文介绍了PHP警告-非法字符串偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我不明白为什么这不起作用。我正在使用WordPress和一个名为ACF的插件来填充一些数据。我循环遍历我的中继器字段,如下所示:

<?php

    $links = get_field('footer_links');  // spits out the array

    if($links) {
        foreach ( $links as $link ) {
            $logo = $link['logo'];
            $link = $link['link'];
            $text = $link['text'];

            echo '<div class="link">';
            echo '  <a href="'.$link.'"><img src="'.$logo.'" /><p>'.$text.'</p></a>';
            echo '</div>';
        }
    }
    // Logo spits out a image path
    // link spits out the URL path
    // text SHOULD just spit out the title, however throws PHP warning

?>

对于变量$text,我收到一条PHP警告。

警告:中的字符串偏移量‘Text’非法...

为什么我的其他变量--<[2-1]、$link没有抛出此警告?它们的创建方式与其他$text变量相同。

我已尝试重新创建整个中继器字段并更改名称等。没有成功。

这是我的print_r($link);

Array
(
    [0] Array
        (
            [logo] http://domainname.com/imagepath
            [link] http://.....
            [text] Text1
        )

    [1] Array
        (
            [logo] http://domainname.com/imagepath
            [link] http://.....
            [text] Text2
        )

    [2] Array
        (
            [logo] http://domainname.com/imagepath
            [link] http://.....
            [text] Text3
        )

)

推荐答案

此处抛出错误:

$link['text'];

数组$link缺少索引['text']。这是因为您在这里覆盖了数组变量:

$link = $link['link'];

更改为:

$href = $link['link'];

你是金子。

这篇关于PHP警告-非法字符串偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆