我正在尝试将此功能用于图像导航:
function array_key_relative($array, $current_key, $offset = 1, $strict = true) {
// create key map
$keys = array_keys($array);
// find current key
$current_key_index = array_search($current_key, $keys, $strict);
// return desired offset, if in array, or false if not
if(isset($keys[$current_key_index + $offset])) {
return $keys[$current_key_index + $offset];
}
return false;
}
我想这样使用它:
<?php
$images = array();
foreach ($images as $key => $image)
$prev_key = $this->array_key_relative($images, $key, -1);
$next_key = $this->array_key_relative($images, $key, 1);
?>
<a href="<?php echo "image?id=".$images[$prev_key]->id; ?>">Prev</a>
<a href="<?php echo "image?id=".$images[$next_key]->id; ?>">Next</a>
问题是当我按 Next 或 Prev 链接它只工作一次时,例如,如果当前键为 1,如果我按 next 将转到 2,但是一旦我在 2 页上导航停止工作(不转到 3, 4、5 等)。谁能指出我正确的方向?谢谢你。