Home » Php » php – JSON: Display JSON result with different format

php – JSON: Display JSON result with different format

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m just new in JSON. Based on my question, below is my current code ad result

Code 1

<?php 

    require_once '../config/configPDO.php';

    header('Content-Type: application/json');

    $response = array();

    $badgeid = '10010080';
    $pwd = '10010080';

    $stmt = $conn->prepare("SELECT * FROM ot_users WHERE badgeid = '$badgeid' AND pwd = '$pwd' AND roles_id = 7 AND team_id <> 1");
    $stmt->execute();
    $result = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!empty($result)) {

        $response['error'] = false; 
        $response['message'] = 'Login successfull'; 
        $response['user'] = $result;  

    }else{
        $response['error'] = false; 
        $response['message'] = 'Invalid username or password';
    }


    echo json_encode($response);

?>

Result JSON 1

{“error”:false,”message”:”Login successfull”,”user”:{“badgeid”:”10010080″,”email”:null,”pwd”:”10010080″,”fullname”:”AZWAN BIN SANIMIN”,”roles_id”:”7″,”team_id”:”2″,”users_id”:null}}

Code 2

<?php 

    header('Content-Type: application/json');

    $response = array();

    $badgeid = '10010080';
    $pwd = '10010080';

    $url = "http://172.20.0.45/TGWebService/TGWebService.asmx/ot_displayUser?badgeid=$badgeid&pwd=$pwd";
    $data = file_get_contents($url);
    $json = json_decode($data);
    $result = $json->otUserList;

        if (!empty($result)) {

            $response['error'] = false; 
            $response['message'] = 'Login successfull'; 
            $response['user'] = $result;  

        }else{
            $response['error'] = false; 
            $response['message'] = 'Invalid username or password';
        }

        echo json_encode($response);

?>

Result JSON 2

{“error”:false,”message”:”Login successfull”,”user”:[{“badgeid”:”10010080″,”email”:””,”pwd”:””,”fullname”:”AZWAN BIN SANIMIN”,”roles_id”:”7″,”team_id”:”2″,”users_id”:””}]}

One of the differences is “[” where result JSON 2 have it while Result 1 Doesn’t. What I want here I want the Result 2 same as result 1.

Can anyone knows where I need to change the code at Code 2?

Thanks.

How to&Answers:

Just php variable changed $result to $result[0] try this. can’t excute your ‘http://172.20.0.45/TGWebService/TGWebService.asmx/ot_displayUser?badgeid=$badgeid&pwd=$pwd’ link because incorrect passowrd

    header('Content-Type: application/json');

    $response = array();

    $badgeid = '10010080';
    $pwd = '10010080';

    $url = "http://172.20.0.45/TGWebService/TGWebService.asmx/ot_displayUser?badgeid=$badgeid&pwd=$pwd";
    $data = file_get_contents($url);
    $json = json_decode($data);
    $result = $json->otUserList;

        if (!empty($result)) {

            $response['error'] = false; 
            $response['message'] = 'Login successfull'; 
            $response['user'] = $result[0];  

        }else{


       $response['error'] = false; 
        $response['message'] = 'Invalid username or password';
    }

    echo json_encode($response);

Answer:

Welcome, @PeterSondak. Have you already seen the json documentation?

I suppose that if you test your $result variable with a var_dump, your result will be something like this:

object(stdClass)#1 (3) {
  ["error"]=>
  bool(false)
  ["message"]=>
  string(17) "Login successfull"
  ["user"]=>
  array(1) {
    [0]=>
    object(stdClass)#2 (7) {
      ["badgeid"]=>
      string(8) "10010080"
      ["email"]=>
      string(0) ""
      ["pwd"]=>
      string(0) ""
      ["fullname"]=>
      string(17) "AZWAN BIN SANIMIN"
      ["roles_id"]=>
      string(1) "7"
      ["team_id"]=>
      string(1) "2"
      ["users_id"]=>
      string(0) ""
    }
  }
}

As you can see, the value for the user attribute is a array. So you should change your code to something like this to get the first index:

$result = $json->otUserList[0]

And you’ll get:

"{"error":"false","message":"Login successfull","user":{"badgeid":"10010080","email":"","pwd":"","fullname":"AZWAN BIN SANIMIN","roles_id":"7","team_id":"2","users_id":""}}"