php - 将 GET 响应与变量进行比较
问题描述
我无法将 get 请求返回的值与变量进行比较。get 以包含数据库名称中的行和值的格式返回。由于变量也没有那个“标签”,所以它们不能正确比较。有没有办法正确比较它们?
{"email":"[value]"}
这就是它返回的格式。值在一个数组中。
$app->get('/api/user_info/{email}', function($request, $response) {
require_once('dbconnect.php');
$email = $request->getAttribute('email');
$query = "select email from user_info";
$result = $mysqli->query($query);
$count = 0;
while($row = $result->fetch_assoc()){
$data[] = $row;
}
for ($i=0; $i < count($data); $i++){
if($data[$i] == ($email)){
$newResponse = $response->withjson($data[$i]);
return $newResponse;
//Should return email if they're the same
}
else {
$count += 1;
}
if ($count == count($data)){
$newResponse = $response->withjson("Email not found");
return $newResponse;
}
}
});
解决方案
json_decode( '{"email":"[value]"}' )
将为您提供一个带有“email”属性的标准 PHP 对象。
我猜想使用什么 GET 键,但这应该可以帮助您弄清楚:
$var = json_decode( $_GET['email'] );
echo $var->email;
推荐阅读
- python - 如何对二进制字符串列表进行排序
- android - 如何全屏打开侧边导航
- html - 如何制作带有标签a的下拉菜单?
- javascript - 根据条件将数组的元素替换为另一个数组的元素
- ruby - 如何使用 Ruby fiddle 加载 Windows DLL 文件?
- bash - 通过编辑相同的变量来设置变量
- swift - 是否可以以编程方式更新在函数中更新的 UILabel 文本,然后通过选择器调用该文本?
- neo4j - 计数集合时计数不正确
- typescript - 数组查找方法错误元素隐式具有“任何”类型
- php - 如何从我需要在控制器中的 CodeIgniter 中的配置文件中获取变量