Announcement

Collapse
No announcement yet.

JSON Response to PHP Array

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • JSON Response to PHP Array

    Hi everybody,
    I'm trying a curl request , this is my code
    PHP Code:
    $voltdbserver "http://apvv-volt-poc:8080/api/1.0/";
               
    $proc "SELECT2";
             
             
    $a = array($username,$password);
             
             
    $params json_encode($a);
             
    $params urlencode($params);
             
    $querystring "Procedure=$proc&Parameters=$params";
             
            
    $chcurl_init();
             
            
    curl_setopt($chCURLOPT_URL$voltdbserver);
               
    curl_setopt($chCURLOPT_HEADER0);
               
    curl_setopt($chCURLOPT_FAILONERROR1);
               
    curl_setopt($chCURLOPT_POST1);
               
    curl_setopt($chCURLOPT_POSTFIELDS$querystring);
               
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);

            
    // Execute the request
            //curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
               
    $result curl_exec($ch);
            
            
    curl_close($ch);
            
            
    $json json_decode($result);
            
    //$json = json_decode($result, true);
            

            
    print_r($json); 
    The json response I got is:
    Code:
    {"status":1,"appstatus":-128,"statusstring":null,"appstatusstring":null,"results":[{"status":-128,"schema":[{"name":"ID","type":5},{"name":"USER_NAME","type":9},{"name":"DISPLAY_NAME","type":9},{"name":"PASSWORD","type":9},{"name":"EMAIL","type":9},{"name":"ACTIVATION_TOKEN","type":9},{"name":"LAST_ACTIVATION_REQUEST","type":5},{"name":"LOST_PASSWORD_REQUEST","type":5},{"name":"ACTIVE","type":5},{"name":"TITLE","type":9},{"name":"SIGN_UP_STAMP","type":5},{"name":"LAST_SIGN_IN_STAMP","type":5}],"data":[[10,"Ghalia","ghalia","ghalia","ghalia@mail","6",7,8,9,"10",11,12]]}]}
    the part I need is just the array "data":[[10,"Ghalia","ghalia","ghalia","ghalia@mail","6",7, 8,9,"10",11,12]]
    How can I convert it to php array? I tried
    Code:
     $user = $json['results']['data'];
    print_r($user);
    but it dosn't work.

  • #2
    Because "results" and "data" fields contain arrays, you need to access the data array like this,
    PHP Code:
    print_r($json->results[0]->data[0]); 
    We have a native PHP extension that is more efficient and easier to use. You can download it from http://voltdb.com/community/monitoring-tools .
    Ning

    Comment


    • #3
      Thank you so much :)

      Comment


      • #4
        Use json_decode, found here: http://php.net/manual/en/function.json-decode.php.

        Sample:

        $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

        var_dump(json_decode($json));

        Comment

        Working...
        X