问题描述
我想3个参数传递给一个脚本,其中第三个参数 $ _ GET ['VALUE3']
应该是一个数组
I'm trying to pass 3 parameter to a script, where the 3rd parameter $_GET['value3']
is supposed to be an array
$_GET['value1']
$_GET['value2']
$_GET['value3'] //an array of items
我打电话这样的脚本:(注意我的 VALUE3
,我不知道这是正确的语法)
I'm calling the script like this: (notice my syntax for value3
, I'm not sure it's correct)
http://localhost/test.php?value1=test1&value2=test2&value3=[the, array, values]
我再通过第三个参数使用foreach来循环希望 VALUE3
这是阵列
//process the first input $_GET['value1']
//process the second input $_GET['value2']
//process the third input $_GET['value3'] which is the array
foreach($_GET['value3'] as $arrayitem){
echo $arrayitem;
}
但我得到的错误的foreach为无效的论点提供()
我不知道如果我的方法是正确的。能澄清一些你如何去这样做之类的事情。
I'm not sure if my methodology is correct. Can some clarify how you'd go about doing the sort of thing
推荐答案
有作为传递一个数组作为URL参数(或窗体值,对于这个问题,因为这是同样的事情)没有这样的事。这些都是字符串,以及任何发生在他们超越那就是已经被内置到应用程序服务器的魔力,因此,它是不可移植的。
There is no such thing as "passing an array as a URL parameter" (or a form value, for that matter, because this is the same thing). These are strings, and anything that happens to them beyond that is magic that has been built into your application server, and therefore it is non-portable.
PHP恰好支持&放大器; VALUE3 [] =的&放大器; VALUE3 [] =阵列和放大器; VALUE3 [] =值
标记,自动将创建 $ _GET ['VALUE3']
作为你的一个数组,但这是特别的PHP,并不一定在其他地方工作。
PHP happens to support the &value3[]=the&value3[]=array&value3[]=values
notation to automagically create $_GET['value3']
as an array for you, but this is special to PHP and does not necessarily work elsewhere.
您也可以直接的,去一个更清洁的URL,如下所示: VALUE3 =的,数组,值
,然后用爆炸('',$ _GET ['VALUE3'])
在你的PHP脚本来创建一个数组。当然,这意味着你的分隔符字符不能是值的一部分。
You can also be straight-forward and go for a cleaner URL, like this: value3=the,array,values
, and then use explode(',', $_GET['value3'])
in your PHP script to create an array. Of course this implies that your separator char cannot be part of the value.
要明确地传输结构化数据通过HTTP,使用已取得为目的的格式(即:JSON),然后使用 json_de code()
上PHP端。
To unambiguously transport structured data over HTTP, use a format that has been made for the purpose (namely: JSON) and then use json_decode()
on the PHP side.
这篇关于一个$ _GET输入参数是一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!