我有一个数组,我想获得下一个值.
$sorting = array(0 => "H 101", 1 => "S 101", 2 => "R 172", 3 => "D 141", 4 => "T 101", 5 => "K 101", 6 => "A 182", 7 => "G 101");
Run Code Online (Sandbox Code Playgroud)
我从SELECT语句中获取当前值.我已经尝试过array_search,但我不能让它想做我想要的.
假设我的当前值是S 101,我怎么能让它给我"R 172"作为下一个值,然后我可以在一个新的SELECT语句中使用它.
如果当前值是最后一个值(G 101),我还希望它回到第一个值(H 101).
在脚本处理时,next()和prev()处理数组的内部指针.
$current_index = array_search($current_id, $array);
// Find the index of the next/prev items
$next = $current_index + 1;
$prev = $current_index - 1;
Run Code Online (Sandbox Code Playgroud)
你的HTML:
<?php if ($prev > 0): ?>
<a href="<?= $array[$prev] ?>">Previous</a>
<?php endif; ?>
<?php if ($next < count($array)): ?>
<a href="<?= $array[$next] ?>">Next</a>
<?php endif; ?>
Run Code Online (Sandbox Code Playgroud)