如何从PHP中的给定键获取数组中的下一个键? [英] How to get next key in an array from given key in php?
本文介绍了如何从PHP中的给定键获取数组中的下一个键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,我有一个数组,其元素为
For example i have an array with the element of following
$array = array(
1 => "a",
3 => "c",
4 => "d",
6 => "f",
);
如果我正在使用
how could i get next key from given key, if I'm using some function like
get_next_key_array($array,1);
it should return 3.
如果是
get_next_key_array($array,4);
it should return 6.
可以做到,我知道next() ,current(),prev()在php中,但是我不知道如何使用默认函数来实现所需的结果。
请帮助我。
Is it can be done, I know that next(),current(),prev() in php, but i don't know how to implement my required result using default functions. Please help me.
推荐答案
尝试一下
function get_next_key_array($array,$key){
$keys = array_keys($array);
$position = array_search($key, $keys);
if (isset($keys[$position + 1])) {
$nextKey = $keys[$position + 1];
}
return $nextKey;
}
这篇关于如何从PHP中的给定键获取数组中的下一个键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文