<?phpforeach($arr as $k => $v) { var_dump(key($arr)."=>".current($arr));}<pre>$arr = [ 'a' => 'q', '2' => 'e', 's' => 'v', 'd' => 'b', ];
输出为
string(4) "2=>e" string(4) "2=>e" string(4) "2=>e" string(4) "2=>e"
key()函数使用在while循环中的,在foreach中有意想不到的错误,汗=_=
本文探讨了 PHP 中 key() 函数在 foreach 循环中的不寻常行为,并通过一个具体的例子展示了如何避免此类陷阱。文章指出,在特定场景下 key() 函数的表现可能与预期不符,尤其是在遍历数组时。

508

被折叠的 条评论
为什么被折叠?



