一个解决方案是使用
end和 (引用)
的组合:
key__
end()
将array的内部指针前进到最后一个元素,并返回其值。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 pointervar_dump($key);
将输出:
string 'last' (length=4)
即我数组的最后一个元素的键。
完成此操作后,数组的内部指针将位于数组的末尾。如注释中所指出的,您可能希望
reset()在数组上运行以将指针带回到数组的开头。



