php - $_GET ['variable' ] 到数组变量中

标签 php arrays get

好的,我有一个用三个值命名的数组:

$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/

相关文章:

php - WordPress - 将 .active 类添加到事件菜单项的子页面?

Php Activerecord,更新对象的 NOW()

php命名空间目录下的所有文件

java - 从 .txt 读入二维数组

java - 不带帖子的 JSOUP Cookie ID

php - 从一个数组中的sql中获取数据

java - String.getBytes() 如何将字符串转换为字节?

javascript - 使用 typescript/javascript 将数组拆分为组

regex - Express.js : how to make app. 在 req.param 中得到 ('/[:userId]i' ..)?

android - Flutter 返回空字符串 - HTTP(S) GET 请求