【问题标题】:php GET method with arrays带有数组的php GET方法
【发布时间】:2018-12-01 08:44:10
【问题描述】:

我是 php 新手, 我想使用 $_GET 和数组获得这些结果:

http://127.0.0.1/get.php?id=0 i want to display to me example0
http://127.0.0.1/get.php?id=1 i want to display to me example0
http://127.0.0.1/get.php?id=2 i want to display to me example0
http://127.0.0.1/get.php?id=3 i want to display to me example0

我试过这个:

<?php
$names=array('example0','example1','example2','example3');
echo $_GET[names];
?>

而且不工作!

【问题讨论】:

  • 嗯?不知道你在问什么
  • 如何在 URL 中传递该数组?
  • 您想使用数组来创建 URL 还是要接受来自 get 参数的数组?

标签: php arrays methods get


【解决方案1】:

这不是繁重的工作,而是更优雅:

$names=array('example0','example1','example2','example3');

$index = $_GET['id'];
echo $names[$index];

你应该在更现实的情况下检查索引是否存在:

$names=array('example0','example1','example2','example3');

$index = $_GET['id'];
if(isset($names[$index])){//Check if index exists
    echo $names[$index];
}

这样您就不必在数组更改时更改代码!

【讨论】:

    【解决方案2】:

    这应该可以解决您的问题。

    if ($_GET['id'] == 0){
      ...
    } elseif ($_GET['id'] == 1){
      ...
    } elseif ($_GET['id'] == 2){
      ...
    } elseif ($_GET['id'] == 3){
      ...
    }
    

    【讨论】:

    • 哦哈哈他们就是这么问的!
    猜你喜欢
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-23
    • 2015-06-30
    • 1970-01-01
    • 2018-05-08
    • 1970-01-01
    相关资源
    最近更新 更多