简体   繁体   English

未定义的偏移量错误,但偏移量未定义

[英]Undefined offset error, but offset is not undefined

I'm getting: 我越来越:

Notice: Undefined offset: 0 

in my code, however I can print_r the element I am trying to get and its clearly defined. 在我的代码中,但是我可以print_r我想要得到的元素和明确定义的元素。

function get_members($entries_found) {
   $members = $entries_found[0]['member'];
   ...
}

If I print_r($members) I get the expected output, however I'm still getting the Notice. 如果我print_r($ members)我得到了预期的输出,但是我仍然收到通知。

Any clues? 有线索吗?

Do

var_dump($entries_found);

To check that the array does indeed have an offset of zero. 检查数组是否确实具有零偏移量。 Other things you can try would be reseting the array pointer 您可以尝试的其他事情是重置数组指针

reset($entries_found);

of checking if it's set first 检查是否先设置

if (isset($entries_found[0]['member'])) // do things

If all else fails you could just supress the notice with 如果所有其他方法都失败了,你可以用通知来压制通知

$members = @$entries_found[0]['member'];

I don't really know what happens with your $entries_found before accessing it from get_members 在从get_members访问之前,我真的不知道你的$entries_found会发生什么

But i had the same problem. 但我有同样的问题。 print_r and var_dump showed me, that the index exists but when i tried to access it i got the offset error print_rvar_dump告诉我,索引存在,但是当我试图访问它时,我得到了offset error

In my case i decoded a json string with json_decode without setting the assoc flag. 在我的情况下,我使用json_decode解码了一个json字符串,而没有设置assoc标志。

// Not working
$assocArray = json_decode('{"207":"sdf","210":"sdf"}');
echo $assocArray[207];


// working witht the assoc flag set
$assocArray = json_decode('{"207":"sdf","210":"sdf"}', true);
echo $assocArray[207];

Got my solution from here: Undefined offset while accessing array element which exists 从这里得到我的解决方案: 访问存在的数组元素时未定义的偏移量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM