php - 使用inval()转换索引时未定义的偏移量:0错误

时间:2022-09-18 08:36:37

I'm trying to access values of my array, but getting very strange behavior.

我正在尝试访问我的数组的值,但是获得了非常奇怪的行为。

This doesn't work and throws exception Undefined offset: 0:

这不起作用并抛出异常Undefined offset:0:

$k = $sp["id"]; 
$k = intval($k); //$k prints out as int(117) here
$ship->name = $vessels[$k];

This works:

这有效:

$k = 117;
$ship->name = $vessels[$k];

var_dump($sp["id"]) gives me string(3) "117"

var_dump($ sp [“id”])给我字符串(3)“117”

Can anyone explain me how is it possible?

任何人都可以解释我怎么可能?

1 个解决方案

#1


0  

Just try

你试一试

(int)$sp["id"]

It's also faster :)

它也更快:)

#1


0  

Just try

你试一试

(int)$sp["id"]

It's also faster :)

它也更快:)