print_r($p->attachments)
产生:
Array
(
[0] => stdClass Object
(
[id] => ...
[url] => http://...png
[slug] => ...
[title] => ...
[description] => ...
[caption] => ...
[parent] => ...
[mime_type] => image/png
[images] => ...
(
)
)
)
我希望访问该url
字段中的值
print_r($p->attachments[0]->url)
检索URL,但还会产生: Undefined offset: 0
现在,我可以通过调用来消除错误print_r(@$p->attachments[0]->url)
,但是是否有解决此问题的正确方法?
我无法修改$ p对象。
编辑:
如建议的那样,这是Var_dump($ p-> attachments)的响应
array(1) {
[0]=>
object(stdClass)#322 (9) {
["id"]=>
int(1814)
["url"]=>
string(76) "..."
["slug"]=>
string(34) "..."
["title"]=>
string(34) "..."
["description"]=>
string(0) ""
["caption"]=>
string(53) "..."
["parent"]=>
int(1811)
["mime_type"]=>
string(9) "image/png"
["images"]=>
array(0) {
}
}
}
您可以使用isset()检查数组:
if(isset($p->attachments[0])){
echo $p->attachments[0]->url;
}
else {
//some error?
}
或者,如果您知道只检查索引0,则可以这样做
$array = $array + array(null);
因此,如果未设置原始$ array [0],则现在为null
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句