好的,我有一个用三个值命名的数组:
$tutorials = array('introduction', 'get_started', 'basics')
我还有一个链接,例如:
mysite.com/?tutorials=get_started
所以上面的链接似乎是 $tutorials 的第一个值,但是如果我希望我的 anchor 的 href 与下一个值类似怎么办?
<a href="?tutorials=basics">Next</a>
这有什么捷径吗?因为我的数组不只是3个而是20个,我不想一一编辑它们。
我在这里尝试做的是下一个和上一个链接。请帮忙。
谢谢!
最佳答案
这样的事情应该有效:
<?php
$value = $_GET['tutorials']; // get the current
// find it's position in the array
$key = array_search( $value, $tutorials );
if( $key !== false ) {
if( $key > 0 ) // make sure previous doesn't try to search below 0
$prev_link = '?tutorials=' . $tutorials[$key-1];
if( $key < count( $tutorials ) ) // Make sure we dont go beyond the end of the array
$next_link = '?tutorials=' . $tutorials[$key+1];
} else {
// Some logic to handle an invalid key (not in the array)
}
?>
关于php - $_GET ['variable' ] 到数组变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10583139/