如何获取 PHP $_GET 数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1833330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:59:25  来源:igfitidea点击:

How to get PHP $_GET array?

php

提问by faya

Is it possible to have a value in $_GETas an array?

是否可以将值$_GET作为数组?

If I am trying to send a link with http://link/foo.php?id=1&id=2&id=3, and I want to use $_GET['id']on the php side, how can that value be an array? Because right now echo $_GET['id']is returning 3. Its the last id which is in the header link. Any suggestions?

如果我试图发送一个带有 的链接http://link/foo.php?id=1&id=2&id=3,并且我想$_GET['id']在 php 端使用,那么该值怎么可能是一个数组?因为此时echo $_GET['id']正在回归3。它是标题链接中的最后一个 id。有什么建议?

回答by Jordan Running

The usual way to do this in PHP is to put id[]in your URL instead of just id:

在 PHP 中执行此操作的通常方法是id[]输入您的 URL 而不仅仅是id

http://link/foo.php?id[]=1&id[]=2&id[]=3

Then $_GET['id']will be an array of those values. It's not especially pretty, but it works out of the box.

然后$_GET['id']将是这些值的数组。它不是特别漂亮,但它开箱即用。

回答by Sampson

You could make ida series of comma-seperated values, like this:

您可以创建id一系列以逗号分隔的值,如下所示:

index.php?id=1,2,3&name=john

index.php?id=1,2,3&name=john

Then, within your PHP code, explode it into an array:

然后,在您的 PHP 代码中,将其分解为一个数组:

$values = explode(",", $_GET["id"]);
print count($values) . " values passed.";

This will maintain brevity. The other (more commonly used with $_POST) method is to use array-style square-brackets:

这将保持简洁。另一种(更常与 $_POST 一起使用)方法是使用数组样式的方括号:

index.php?id[]=1&id[]=2&id[]=3&name=john

index.php?id[]=1&id[]=2&id[]=3&name=john

But that clearly would be much more verbose.

但这显然会更加冗长。

回答by Lucas Oman

You can specify an array in your HTML this way:

您可以通过这种方式在 HTML 中指定一个数组:

<input type="hidden" name="id[]" value="1"/>
<input type="hidden" name="id[]" value="2"/>
<input type="hidden" name="id[]" value="3"/>

This will result in this $_GET array in PHP:

这将导致 PHP 中的 $_GET 数组:

array(
  'id' => array(
    0 => 1,
    1 => 2,
    2 => 3
  )
)

Of course, you can use any sort of HTML input, here. The important thing is that all inputs whose values you want in the 'id' array have the name id[].

当然,您可以在这里使用任何类型的 HTML 输入。重要的是,您希望在 'id' 数组中具有其值的所有输入都具有 name id[]

回答by ChronoFish

You can get them using the Query String:

您可以使用查询字符串获取它们:

$idArray = explode('&',$_SERVER["QUERY_STRING"]);

This will give you:

这会给你:

$idArray[0] = "id=1";
$idArray[1] = "id=2";
$idArray[2] = "id=3";

Then

然后

foreach ($idArray as $index => $avPair)
{
  list($ignore, $value) = explode("=", $avPair);
  $id[$index] = $value;
}

This will give you

这会给你

$id[0] = "1";
$id[1] = "2";
$id[2] = "3";

回答by ChristopheD

When you don't want to change the link (e.g. foo.php?id=1&id=2&id=3) you could probably do something like this (although there might be a better way...):

当您不想更改链接(例如foo.php?id=1&id=2&id=3)时,您可能会执行以下操作(尽管可能有更好的方法...):

$id_arr = array();
foreach (explode("&", $_SERVER['QUERY_STRING']) as $tmp_arr_param) {
    $split_param = explode("=", $tmp_arr_param);
    if ($split_param[0] == "id") {
        $id_arr[] = urldecode($split_param[1]);
    }
}
print_r($id_arr);

回答by Michael J. Calkins

Put all the ids into a variable called $ids and separate them with a ",":

将所有 id 放入一个名为 $ids 的变量中,并用“,”分隔它们:

$ids = "1,2,3,4,5,6";

Pass them like so:

像这样传递它们:

$url = "?ids={$ids}";

Process them:

处理它们:

$ids = explode(",", $_GET['ids']);

回答by robjmills

I think i know what you mean, if you want to send an array through a URL you can use serialize

我想我知道你的意思,如果你想通过 URL 发送一个数组,你可以使用序列化

for example:

例如:

$foo = array(1,2,3);
$serialized_array = serialize($foo);
$url = "http://www.foo.whatever/page.php?vars=".urlencode($serialized_array);

and on page.php

并在 page.php

$vars = unserialize($_GET['vars']);

回答by raju

Yes, here's a code example with some explanation in comments:

是的,这是一个代码示例,注释中有一些解释:

<?php
 // Fill up array with names

$sql=mysql_query("SELECT * FROM fb_registration");
while($res=mysql_fetch_array($sql))
{


  $a[]=$res['username'];
//$a[]=$res['password'];
}

//get the q parameter from URL
$q=$_GET["q"];

//lookup all hints from array if length of q>0

if (strlen($q) > 0)
  {
  $hint="";
  for($i=0; $i<count($a); $i++)
    {
    if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
      {
      if ($hint=="")
        {
        $hint=$a[$i];
        }
      else
        {
        $hint=$hint." , ".$a[$i];
        }
      }
    }
  }
?>