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

发布于 2024-09-08 22:03:22 字数 295 浏览 6 评论 0原文

我得到:

Notice: Undefined offset: 0 

在我的代码中,但是我可以 print_r 我试图获取的元素及其明确定义的。

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

如果我 print_r($members) 我得到预期的输出,但是我仍然收到通知。

有什么线索吗?

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.

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.

Any clues?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

给妤﹃绝世温柔 2024-09-15 22:03:22

检查

var_dump($entries_found);

数组的偏移量是否确实为零。您可以尝试的其他方法是重置数组指针,

reset($entries_found);

检查它是否首先设置

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

如果所有其他方法都失败,您可以使用以下命令来抑制通知

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

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'];
樱花细雨 2024-09-15 22:03:22

在从 get_members 访问您的 $entries_found 之前,我真的不知道它会发生什么,

但我也遇到了同样的问题。 print_rvar_dump 向我展示了索引存在,但是当我尝试访问它时,我收到了 offset error

在我的例子中,我解码了一个 json带有 json_decode 的字符串,而不设置 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];

从这里得到了我的解决方案: 访问存在的数组元素时未定义偏移

I don't really know what happens with your $entries_found before accessing it from get_members

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

In my case i decoded a json string with json_decode without setting the assoc flag.

// 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文