php 将数组作为 url 参数传递
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1763508/
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
Passing arrays as url parameter
提问by uji
What is the best way that I can pass an array as a url parameter? I was thinking if this is possible:
我可以将数组作为 url 参数传递的最佳方式是什么?我在想这是否可能:
$aValues = array();
$url = 'http://www.example.com?aParam='.$aValues;
or how about this:
或者这个怎么样:
$url = 'http://www.example.com?aParam[]='.$aValues;
Ive read examples, but I find it messy:
我读过例子,但我觉得它很乱:
$url = 'http://www.example.com?aParam[]=value1&aParam[]=value2&aParam[]=value3';
回答by Stefan Gehrig
There is a very simple solution: http_build_query(). It takes your query parameters as an associative array:
有一个非常简单的解决方案:http_build_query(). 它将您的查询参数作为关联数组:
$data = array(
1,
4,
'a' => 'b',
'c' => 'd'
);
$query = http_build_query(array('aParam' => $data));
will return
将返回
string(63) "aParam%5B0%5D=1&aParam%5B1%5D=4&aParam%5Ba%5D=b&aParam%5Bc%5D=d"
http_build_query()handles all the necessary escaping for you (%5B=> [and %5D=> ]), so this string is equal to aParam[0]=1&aParam[1]=4&aParam[a]=b&aParam[c]=d.
http_build_query()为您处理所有必要的转义(%5B=>[和%5D=> ]),因此该字符串等于aParam[0]=1&aParam[1]=4&aParam[a]=b&aParam[c]=d.
回答by Jordan Running
Edit:Don't miss Stefan's solution above, which uses the very handy http_build_query()function: https://stackoverflow.com/a/1764199/179125
编辑:不要错过上面 Stefan 的解决方案,它使用了非常方便的http_build_query()功能:https: //stackoverflow.com/a/1764199/179125
knittl is right on about escaping. However, there's a simpler way to do this:
knittl 关于逃避是正确的。但是,有一种更简单的方法可以做到这一点:
$url = 'http://example.com/index.php?';
$url .= 'aValues[]=' . implode('&aValues[]=', array_map('urlencode', $aValues));
If you want to do this with an associative array, try this instead:
如果你想用关联数组来做这件事,试试这个:
PHP 5.3+(lambda function)
PHP 5.3+(lambda 函数)
$url = 'http://example.com/index.php?';
$url .= implode('&', array_map(function($key, $val) {
return 'aValues[' . urlencode($key) . ']=' . urlencode($val);
},
array_keys($aValues), $aValues)
);
PHP <5.3(callback)
PHP <5.3(回调)
function urlify($key, $val) {
return 'aValues[' . urlencode($key) . ']=' . urlencode($val);
}
$url = 'http://example.com/index.php?';
$url .= implode('&', array_map('urlify', array_keys($aValues), $aValues));
回答by nash
Easiest way would be to use the serializefunction.
最简单的方法是使用该serialize功能。
It serializes any variable for storage or transfer. You can read about it in the php manual - serialize
它序列化任何变量以进行存储或传输。您可以在php 手册中阅读有关它的信息- 序列化
The variable can be restored by using unserialize
可以通过使用恢复变量 unserialize
So in the passing to the URL you use:
因此,在传递给您使用的 URL 时:
$url = urlencode(serialize($array))
$url = urlencode(serialize($array))
and to restore the variable you use
并恢复您使用的变量
$var = unserialize(urldecode($_GET['array']))
$var = unserialize(urldecode($_GET['array']))
Be careful here though. The maximum size of a GET request is limited to 4k, which you can easily exceed by passing arrays in a URL.
不过这里要小心。GET 请求的最大大小限制为 4k,您可以通过在 URL 中传递数组轻松超过该大小。
Also, its really not quite the safest way to pass data! You should probably look into using sessions instead.
此外,它真的不是最安全的数据传递方式!您可能应该考虑使用会话。
回答by knittl
please escape your variables when outputting (urlencode).
请在输出 ( urlencode)时转义您的变量。
and you can't just print an array, you have to build your url using a loop in some way
你不能只打印一个数组,你必须以某种方式使用循环来构建你的 url
$url = 'http://example.com/index.php?'
$first = true;
foreach($aValues as $key => $value) {
if(!$first) $url .= '&';
else $first = false;
$url .= 'aValues['.urlencode($key).']='.urlencode($value);
}
回答by Thusitha Sumanadasa
<?php
$array["a"] = "Thusitha";
$array["b"] = "Sumanadasa";
$array["c"] = "Lakmal";
$array["d"] = "Nanayakkara";
$str = serialize($array);
$strenc = urlencode($str);
print $str . "\n";
print $strenc . "\n";
?>
print $str . "\n";gives a:4:{s:1:"a";s:8:"Thusitha";s:1:"b";s:10:"Sumanadasa";s:1:"c";s:6:"Lakmal";s:1:"d";s:11:"Nanayakkara";}and
print $str . "\n";给a:4:{s:1:"a";s:8:"Thusitha";s:1:"b";s:10:"Sumanadasa";s:1:"c";s:6:"Lakmal";s:1:"d";s:11:"Nanayakkara";}和
print $strenc . "\n";gives
print $strenc . "\n";给
a%3A4%3A%7Bs%3A1%3A%22a%22%3Bs%3A8%3A%22Thusitha%22%3Bs%3A1%3A%22b%22%3Bs%3A10%3A%22Sumanadasa%22%3Bs%3A1%3A%22c%22%3Bs%3A6%3A%22Lakmal%22%3Bs%3A1%3A%22d%22%3Bs%3A11%3A%22Nanayakkara%22%3B%7D
So if you want to pass this $arraythrough URL to page_no_2.php,
因此,如果您想$array通过 URL 将其传递给page_no_2.php,
ex:-
前任:-
$url ='http://page_no_2.php?data=".$strenc."';
To return back to the original array, it needs to be urldecode(), then unserialize(), like this in page_no_2.php:
要返回原始数组,它需要是urldecode(), then unserialize(),就像在 page_no_2.php 中这样:
<?php
$strenc2= $_GET['data'];
$arr = unserialize(urldecode($strenc2));
var_dump($arr);
?>
gives
给
array(4) {
["a"]=>
string(8) "Thusitha"
["b"]=>
string(10) "Sumanadasa"
["c"]=>
string(6) "Lakmal"
["d"]=>
string(11) "Nanayakkara"
}
again :D
再次:D
回答by Viktor
I do this with serialized data base64 encoded. Best and smallest way, i guess. urlencode is to much wasting space and you have only 4k.
我用序列化的数据库 base64 编码来做到这一点。最好和最小的方式,我猜。urlencode 浪费了太多空间,而您只有 4k。
回答by Thomas Williams
This isn't a direct answer as this has already been answered, but everyone was talking about sending the data, but nobody really said what you do when it gets there, and it took me a good half an hour to work it out. So I thought I would help out here.
这不是一个直接的答案,因为这已经得到了回答,但是每个人都在谈论发送数据,但是没有人真正说到达那里时你做了什么,我花了半个小时才弄明白。所以我想我会在这里帮忙。
I will repeat this bit
我会重复这一点
$data = array(
'cat' => 'moggy',
'dog' => 'mutt'
);
$query = http_build_query(array('mydata' => $data));
$query=urlencode($query);
Obviously you would format it better than this www.someurl.com?x=$query
显然你会比这个 www.someurl.com?x=$query 更好地格式化它
And to get the data back
并取回数据
parse_str($_GET['x']);
echo $mydata['dog'];
echo $mydata['cat'];
回答by user2638158
**in create url page**
$data = array(
'car' => 'Suzuki',
'Model' => '1976'
);
$query = http_build_query(array('myArray' => $data));
$url=urlencode($query);
echo" <p><a href=\"index2.php?data=".$url."\"> Send </a><br /> </p>";
**in received page**
parse_str($_GET['data']);
echo $myArray['car'];
echo '<br/>';
echo $myArray['model'];

