开发者

Does PHP handle multi-dimension $_REQUEST arrays?

开发者 https://www.devze.com 2023-03-02 20:20 出处:网络
A great feature of PHP is that it handles arrays in request variables, so if you post a string like ...&test[one]=two&test[three]=four then you can access test as an array by using $_REQUEST[\

A great feature of PHP is that it handles arrays in request variables, so if you post a string like ...&test[one]=two&test[three]=four then you can access test as an array by using $_REQUEST['test'].

However I've discovered today that there's a problem with multi-dimension $_REQUEST arrays, and I'm wondering if there's a way around it.

To test, I used a form with the fields:

<in开发者_Go百科put name="one[one]" />
<input name="one[two]" />
<input name="three[four[five]]" />
<input name="three[four[six]]" />

Once this was submitted, I used var_dump to see the array structure:

array(3) {
  ["one"]=>
  array(2) {
    ["one"]=>
    string(0) ""
    ["two"]=>
    string(0) ""
  }
  ["three"]=>
  array(2) {
    ["four[five"]=>
    string(0) ""
    ["four[six"]=>
    string(0) ""
  }
}

The one element is array-ified as expected. But the three element is not. Now I wouldn't mind if it just wasn't supported, but what's got me confused is the reason why. Look at the names of the sub-elements of three - four[five and four[six. It's not being interpreted as an array because for some reason the trailing ] which would help to identify these elements as array values has been lost!

Does anyone have an explanation for this? Is there any way around it, other than to only use a maximum of one level in $_REQUEST arrays?


Try three[four][five] and three[four][six] instead.


The input names correspond completely to variable "names".

You'd write in PHP:

$three['four']['five'] = 'trees';

Likewise in your HTML:

<input name="three[four][five]" value="trees" />
0

精彩评论

暂无评论...
验证码 换一张
取 消