循环过滤,但只获得了最后一条?-_-"
function request(string $key)
{
$method = strtoupper($_SERVER['REQUEST_METHOD']);
$data = [];
if ($method === "GET") {
foreach ($_GET as $key => $value) {
$data[$key] = escape($value);
}
} elseif ($method === "POST") {
foreach ($_POST as $key => $value) {
$data[$key] = escape($value);
}
}
return $data[$key];
}
输出结果:
Array
(
[email] => 11111111
[username] => 11111111
[password] => 11111111
)
![]() |
1
fuchish112 99 天前
为啥不是 return $data ?
|
![]() |
2
sadfQED2 99 天前 via Android
为啥不直接用$_REQUEST?
|
3
justrand 99 天前
你贴出的输出结果有啥问题吗?
|
![]() |
4
zuokanyunqishi 99 天前 via Android
变量覆盖了?传进来的 key ,循环里的 key ?
|
![]() |
5
westoy 99 天前
因为上下两个 foreach 都把 key 给改到了最后一个键啊
|
6
msojocs 99 天前
直接 `return escape($_REQUEST[$key]);` ?
|
![]() |
7
feiffy 99 天前
为啥不是 return $data ?
|
![]() |
8
wfdaj OP 感谢,断断续续自学,一直是新手中......😂
|