如何得到一个数组最后的关键? [英] How to get last key in an array?

查看:127
本文介绍了如何得到一个数组最后的关键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能得到一个数组的最后一个键?

How can I get the last key of an array?

推荐答案

一个解决方案是使用 结束的组合 (报价)的:

A solution would be to use a combination of end and key (quoting) :


  • 端()进入阵列的内部指针指向最后一个元素,并返回其值。

  • 键()返回当前阵列位置的索引元素。

  • end() advances array 's internal pointer to the last element, and returns its value.
  • key() returns the index element of the current array position.

所以,像这样的人应该做的伎俩code的一部分:

So, a portion of code such as this one should do the trick :

$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)

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

i.e. the key of the last element of my array.

这篇关于如何得到一个数组最后的关键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆