猿问

如何从 URL 获取所有 GET 值?(缺失值)

我正在遍历 GET 值并且只得到 13 个值,无论我提交什么 URL。此外,它没有按顺序获取值...


当我循环时,我只得到 13 个值,以及当我在 $_GET 本身上使用 var_dump 时;即使有更多的值要检索。


这是网址:


website.com/Questionaire.php?SurveyName=TV%20Quiz&SurveyType=MultipleChoice&Q1=Choose%20a%20character%20off%20of%20Happy%20Days?&A1=Benny%20the%20bull&A2=The%20Fonz&A3=Jack%20Cracker&Q3=Favorite%20Friends%20character?&A1=Ross&A2=Monica&A4=Joey&A5=Rachel&A6=Chandler&A7=Phoebe&Q8=Favorite%20Nickelodeon%20show?&A1=Hey%20Arnold!&A2=Rugrats&A8=Ahhhh!%20Real%20Montsters


这是我的结果:


SurveyName: TV Quiz


SurveyType: MultipleChoice


Q1: Choose a character off of Happy Days?


A1: Hey Arnold!


A2: Rugrats


A3: Jack Cracker


Q3: Favorite Friends character?


A4: Joey


A5: Rachel


A6: Chandler


A7: Phoebe


Q8: Favorite Nickelodeon show?


A8: Ahhhh! Real Montsters

如您所见,结果不是按顺序排列的,甚至缺少某些值。


var_dump($_GET);


foreach ($_GET as $key => $value) { 

    echo $key.": ".$value."<br/>\n";

}

我期待这些结果:


SurveyName=TV Quiz


SurveyType=MultipleChoice


Q1=Choose a character off of Happy Days?


A1=Benny the bull //<- missed


A2=The Fonz //<- missed


A3=Jack Cracker


Q3=Favorite Friends character?


A1=Ross //<- missed


A2=Monica //<- missed


A4=Joey


A5=Rachel


A6=Chandler


A7=Phoebe


Q8=Favorite Nickelodeon show?


A1=Hey Arnold!


A2=Rugrats


A8=Ahhhh! Real Montsters


慕后森
浏览 222回答 2
2回答

潇湘沐

查询字符串中不能有相同的参数名称,否则最后一个值将覆盖以前的值。您需要有唯一的答案名称,否则您将丢失数据。您可以想象 PHP$_GET使用以下伪代码添加参数:foreach($param as $key=>$val) {&nbsp; &nbsp; $_GET[$key] = $val;}因此,参数按照它们在请求中首次出现的顺序出现。因此,查询字符串?A=1&B=2&A=3&C=4将A首先出现,然后是B,最后是C。相同参数的最后一个值是使用的值,因此我们得到以下$_GET结果:array(&nbsp; &nbsp; 'A'=>3,&nbsp; &nbsp; 'B'=>2,&nbsp; &nbsp; 'C'=>4);考虑添加问题标识符作为每个答案的前缀。例如,代替A1doQ1A1和Q2A1。这将确保您的数据不会被覆盖。

莫回无

我建议对查询字符串参数名称使用数组表示法,以便保持顺序。就像是:?SurveyName=TV Quiz&SurveyType=MultipleChoice&Q[1]=Choose a character off of Happy Days?&A[1][1]=Benny the bull&A[1][2]=The Fonz&A[1][3]=Jack Cracker&Q[3]=Favorite Friends character?&A[3][1]=Ross&A[3][2]=Monica&A[3][4]=Joey&A[3][5]=Rachel&A[3][6]=Chandler&A[3][7]=Phoebe&Q[8]=Favorite Nickelodeon show?&A[8][1]=Hey Arnold!&A[8][2]=Rugrats&A[8][8]=Ahhhh! Real Montsters当您像这样命名查询字符串参数时,PHP 会将它们解析为数组:array(4) {&nbsp; ["SurveyName"]=>&nbsp; string(7) "TV Quiz"&nbsp; ["SurveyType"]=>&nbsp; string(14) "MultipleChoice"&nbsp; ["Q"]=>&nbsp; array(3) {&nbsp; &nbsp; [1]=>&nbsp; &nbsp; string(37) "Choose a character off of Happy Days?"&nbsp; &nbsp; [3]=>&nbsp; &nbsp; string(27) "Favorite Friends character?"&nbsp; &nbsp; [8]=>&nbsp; &nbsp; string(26) "Favorite Nickelodeon show?"&nbsp; }&nbsp; ["A"]=>&nbsp; array(3) {&nbsp; &nbsp; [1]=>&nbsp; &nbsp; array(3) {&nbsp; &nbsp; &nbsp; [1]=>&nbsp; &nbsp; &nbsp; string(14) "Benny the bull"&nbsp; &nbsp; &nbsp; [2]=>&nbsp; &nbsp; &nbsp; string(8) "The Fonz"&nbsp; &nbsp; &nbsp; [3]=>&nbsp; &nbsp; &nbsp; string(12) "Jack Cracker"&nbsp; &nbsp; }&nbsp; &nbsp; [3]=>&nbsp; &nbsp; array(6) {&nbsp; &nbsp; &nbsp; [1]=>&nbsp; &nbsp; &nbsp; string(4) "Ross"&nbsp; &nbsp; &nbsp; [2]=>&nbsp; &nbsp; &nbsp; string(6) "Monica"&nbsp; &nbsp; &nbsp; [4]=>&nbsp; &nbsp; &nbsp; string(4) "Joey"&nbsp; &nbsp; &nbsp; [5]=>&nbsp; &nbsp; &nbsp; string(6) "Rachel"&nbsp; &nbsp; &nbsp; [6]=>&nbsp; &nbsp; &nbsp; string(8) "Chandler"&nbsp; &nbsp; &nbsp; [7]=>&nbsp; &nbsp; &nbsp; string(6) "Phoebe"&nbsp; &nbsp; }&nbsp; &nbsp; [8]=>&nbsp; &nbsp; array(3) {&nbsp; &nbsp; &nbsp; [1]=>&nbsp; &nbsp; &nbsp; string(11) "Hey Arnold!"&nbsp; &nbsp; &nbsp; [2]=>&nbsp; &nbsp; &nbsp; string(7) "Rugrats"&nbsp; &nbsp; &nbsp; [8]=>&nbsp; &nbsp; &nbsp; string(21) "Ahhhh! Real Montsters"&nbsp; &nbsp; }&nbsp; }}
随时随地看视频慕课网APP
我要回答