循环过滤,但只获得了最后一条?-_-"
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 2022-05-11 11:19:07 +08:00
为啥不是 return $data ?
|
2
sadfQED2 2022-05-11 11:22:55 +08:00 via Android
为啥不直接用$_REQUEST?
|
3
yEhwG10ZJa83067x 2022-05-11 11:32:03 +08:00
你贴出的输出结果有啥问题吗?
|
4
zuokanyunqishi 2022-05-11 12:17:57 +08:00 via Android
变量覆盖了?传进来的 key ,循环里的 key ?
|
5
westoy 2022-05-11 12:29:23 +08:00
因为上下两个 foreach 都把 key 给改到了最后一个键啊
|
6
msojocs 2022-05-11 12:48:18 +08:00
直接 `return escape($_REQUEST[$key]);` ?
|
7
feiffy 2022-05-11 12:50:28 +08:00
为啥不是 return $data ?
|
8
wfdaj OP 感谢,断断续续自学,一直是新手中......😂
|