Get JSON value in PHP

1

I'm having trouble accessing a specific value of a variable that returns a JSON in PHP.

My code is as follows:

//Llama el SP    
$sql="CALL tramites_G(".$valuesInsert.")";
    $resultado = mysqli_query($database,$sql);
           if (!$resultado) {
               die('error');
           } else {
            $jsondata["data"]["tramites"] = array();
            while($row = $resultado->fetch_array(MYSQLI_ASSOC)){
                $jsondata["data"]["tramites"][0] = $row;
            }
            $code = json_encode($jsondata);
            }

    $var = json_decode($code,true);

When doing echo in $jsondata I get the following:

Array ( [data] => Array ( [tramites] => Array ( [0] => Array ( [vtr_id] => 122 ) ) ) )

When doing echo in $code I get the following:

{"data":{"tramites":[{"vtr_id":"122"}]}}

I need to access the pure value of vtr_id to assign it to a variable and continue with a series of validations.

I already try to access with $code[0] but it does not return any value to me. Also with $id= $var->vtr_id; but still not working.

    
asked by Luis Hongo 24.08.2018 в 22:06
source

2 answers

3

you can access that value in this part

while($row = $resultado->fetch_array(MYSQLI_ASSOC)){
    $row["vtr_id"];// la puedes colocar en una variable o en un array con ayuda de array_push() 
    $jsondata["data"]["tramites"][0] = $row;
}

or you can by its address in $code asi $code["data"]["tramites"][0]["vtr_id"]

    
answered by 24.08.2018 в 22:23
1

I already got the courage, thank you very much for your answers.

I enclose the changes I made to the code.

$sql="CALL tramites_G(".$valuesInsert.")";
$resultado = mysqli_query($database,$sql);
       if (!$resultado) {
           die('error');
       } else {
        $jsondata["data"]["tramites"] = array();
        while($row = $resultado->fetch_array(MYSQLI_ASSOC)){
            $jsondata["data"]["tramites"][0] = $row;
            //Obtengo directamente el valor en el while
            $code = $row['vtr_id'];
        }
        }
    
answered by 24.08.2018 в 22:27