您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何获取数组中的最后一个键?

如何获取数组中的最后一个键?

一个解决方案是使用end(引用) 的组合:key __

因此,像这样的一部分代码应该可以解决问题:

$array = array(
    'first' => 123,
    'second' => 456,
    'last' => 789, 
);

end($array);         // move the internal pointer to the end of the array
$key = key($array);  // fetches the key of the element pointed to by the internal pointer

var_dump($key);

输出

string 'last' (length=4)

即我数组的最后一个元素的键。

完成此操作后,数组的内部指针将位于数组的末尾。如注释中所指出的,您可能希望reset()在数组上运行以将指针带回到数组的开头。

其他 2022/1/1 18:14:27 有417人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶