Home  >  Article  >  Backend Development  >  PHP如何处理同重复参数名的参数

PHP如何处理同重复参数名的参数

WBOY
WBOYOriginal
2016-06-06 20:51:401374browse

// http://example.com?p1=v1&p2=v2&p3=v3&p1=v4&p2=v5
var_dump($_GET['p1']; // output v1

这样测试出来只得到v1,开始我以为后来的参数会覆盖前边的参数,结果不是,而且也没有什么错误。
求解释原理

回复内容:

// http://example.com?p1=v1&p2=v2&p3=v3&p1=v4&p2=v5
var_dump($_GET['p1']; // output v1

这样测试出来只得到v1,开始我以为后来的参数会覆盖前边的参数,结果不是,而且也没有什么错误。
求解释原理

PHP的又一2B发明:参数后加[]

<?php $str = "a[]=1&a[]=2";
parse_str($str, $arr);
print_r($arr);

query string可以用parse_url()获取

我得到的结果与你相反

<?php var_export( $_GET );

haoduo.sinaapp.com/app/test.php?a=1&a=2

结果
array (
'a' => '2',
)

你可以使用 $_SERVER["QUERY_STRING"] 获取整个query_string,然后自己写个简单的代码来解析,类似于

$arr_kev = explode('&', $_SERVER['QUERY_STRING']);
$get = array();
foreach ($arr_kev as $kev)
{
    list($key, $val) = explode('=', $kev);
    $get[$key][] = $val;
}

这样就保留了所有信息。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn