php 警告:非法字符串偏移“id”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19637044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Warning: Illegal string offset 'id'
提问by user1444027
I have defined two variables as follows:
我定义了两个变量如下:
$pic = get_tax_meta($books->term_id,'books_field_id', true);
$imageurl = wp_get_attachment_image_src( $pic[id], 'list-thumb' );
print_r($pic)
results in the following:
print_r($pic)
结果如下:
Array ( [id] => 302 [src] => http://localhost/mysite/wp-content/uploads/2013/10/apic.jpg )
However, I get the following warning from $pic[id]:
但是,我从 $pic[id] 收到以下警告:
Warning: Illegal string offset 'id'
Any idea what I'm doing wrong?
知道我做错了什么吗?
回答by user1444027
This seems to have fixed the problem:
这似乎解决了问题:
$pic = get_tax_meta($books->term_id,'books_field_id', true);
if (isset($pic['id'])) {
$picid = $pic['id'];
};
$imageurl = wp_get_attachment_image_src( $picid, 'list-thumb' );
回答by Deepu
You need to wrap the array key in quotes like the following.So This
您需要将数组键用引号括起来,如下所示。所以这
$pic[id]
Must be
必须是
$pic['id']
Or
或者
$pic["id"]
回答by James Donnelly
You need to wrap id
in either single or double quotes:
您需要用id
单引号或双引号括起来:
$pic["id"]
Or:
或者:
$pic['id']
See Accessing array elements with square bracket syntaxfor more detail.
有关更多详细信息,请参阅使用方括号语法访问数组元素。
回答by opalenzuela
Replace
代替
$imageurl = wp_get_attachment_image_src( $pic[id], 'list-thumb' );
with
和
$imageurl = wp_get_attachment_image_src( $pic["id"], 'list-thumb' );