是什么让PHP中的最后添加阵列项目的关键的最好方法? [英] What is the best method of getting the key of the last added array item in PHP?

查看:86
本文介绍了是什么让PHP中的最后添加阵列项目的关键的最好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有更好的办法来做到以下几点:

Is there a better way to do the following:

$array = array('test1', 'test2', 'test3', 'test4', 'test5');
// do a bunch of other stuff, probably a loop
$array[] = 'test6';
end($array);
echo key($array); // gives me 6

这将给最近添加数组元素的关键。

This will give the key of the most recently add array element.

有没有更好的方法来做到这一点?

Is there a better way to do this?

推荐答案

你也可以这样做:

$end = end(array_keys($array));

不过我觉得你的方式做你想要做清楚什么,所以你可以掀起一些像:

But I think your way makes it clear what you want to do, so you could whip something up like:

function array_last_key($array) {
    end($array);
    return key($array);
}

这就是它。

这篇关于是什么让PHP中的最后添加阵列项目的关键的最好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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