首页 > 解决方案 > 将 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;
        }
    }  
}); 

标签: phpapi

解决方案


json_decode( '{"email":"[value]"}' )将为您提供一个带有“email”属性的标准 PHP 对象。

我猜想使用什么 GET 键,但这应该可以帮助您弄清楚:

$var = json_decode( $_GET['email'] );
echo $var->email;

推荐阅读