【发布时间】:2018-03-16 15:06:24
【问题描述】:
我有一个类似于以下的数组:
const BookIndex = array
(
'1' => 'Chapter 1',
'1.1' => 'Chapter 1.1',
'1.1.1' => 'Chapter 1.1.1',
'2' => 'Chapter 2',
'2.1' => 'Chapter 2.1',
'2.1.1' => 'Chapter 2.1.1',
);
假设我以某种方式确定我关心的当前键(位置)是“2”键。如何找到上一个和下一个键?
$CurrentKey = '2';
$CurrentValue = BookIndex[$CurrentKey];
$PreviousKey = null; // I need to figure out the previous key from the current key.
$PreviousValue = BookIndex[$PreviousKey];
$NextKey = null; // I need to figure out the next key from the current key.
$NextValue = BookIndex[$NextKey];
【问题讨论】:
-
这并不难。你有没有尝试过?
-
我尝试了 current、prev 和 next 函数,但它们不起作用,因为当前函数返回数组中的第一项,而不是我选择的起点。