undefined offset php,PHP - undefined offset: 0

问题

print_r($p->attachments) produces:

Array

(

[0] => stdClass Object

(

[id] => ...

[url] => http://...png

[slug] => ...

[title] => ...

[description] => ...

[caption] => ...

[parent] => ...

[mime_type] => image/png

[images] => ...

(

)

)

)

I wish to access the value in the url field

print_r($p->attachments[0]->url) retrieves the url, but also produces: Undefined offset: 0

Now I can supress the error by calling print_r(@$p->attachments[0]->url), but is there a proper way of fixing this?

I am not able to modify the $p object.

Edit:

As suggested, here is response from 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) {

}

}

}

回答1:

You can use isset() for check the array:

if(isset($p->attachments[0])){

echo $p->attachments[0]->url;

}

else {

//some error?

}

Or if you know that you are only going to be checking index 0 then you can do this way

$array = $array + array(null);

So if the original $array[0] was unset, now it is null

回答2:

dunno why it returns you a value and informs you that there is undefined offset 0. strange.

can you check this (since attachments is an array you should do this that way):

if ( count($p->attachments) > 0 ){

foreach ($p->attachments AS $att){

//do the stuff if attachement

}

}

来源:https://stackoverflow.com/questions/18880448/php-undefined-offset-0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Uncaught TypeError: Cannot read properties of undefined (reading 'offset')是一个常见的错误,它表示你尝试访问一个未定义或空对象的属性。在你的引用中,它提到了几个不同的情况,造成这个错误的原因可能有多种。 首先,当你访问一个未定义或空对象的属性时,会出现这个错误。例如,如果你尝试访问一个未定义的对象userInfo的属性data,就会报错。 其次,当你尝试访问一个未定义或空对象的嵌套属性时,也会报错。比如在访问store.user.state.userInfo.data.id时,如果userInfo或data是未定义或空对象,就会出现这个错误。 最后,这个错误也可能与页面的客户端验证有关。当页面打开时,如果出现Uncaught TypeError: Cannot set property __MVC_FormValidation of null错误,可能是因为某些页面的客户端验证无法正常工作。 综上所述,Uncaught TypeError: Cannot read properties of undefined (reading 'offset')这个错误可能是由于访问未定义或空对象的属性,或者页面的客户端验证问题所致。要解决这个错误,你可以检查相关对象是否已定义并包含正确的值,并确保页面的客户端验证正常工作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [解决Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘id‘)报错](https://blog.csdn.net/m0_57524265/article/details/131441971)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [ASP.NET MVC运行出现Uncaught TypeError: Cannot set property __MVC_FormValidation of null的解决方法](https://download.csdn.net/download/weixin_38731761/13069683)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值