Data Retrieval Problem Multidimenssional Array

Asked

Viewed 21 times

1

am having a problem getting data from a json file! Good initially I get Json via CURL POST and I make the Decode with true so it becomes an array:

$data = json_decode(file_get_contents('php://input'));
return (array) $data;
break;

Later I try to find the information that is in the following structure:

{
 "acesso":
     {
      "acessoUsuario":"usuario",
      "acessoSenha":"senha"
     }
}

When returning the data already in array I run $data['access'] and get the following view:

{
 "acessoUsuario":"topsat",
 "acessoSenha":"4501"
}

However when trying the values from within with $date['access']['login'] I get the following return:

Fatal error: Uncaught Error: Cannot use object of type stdClass as array in

Exactly on the line where the $data['access']['access'], this only occurs when I try to access the information more inside than just the first level of the array.

Does anyone know any way I can access this information directly?

  • You say "and I do Code with true", but you don’t call json_decode with the true in the second parameter. Please check?

  • Opa friend, really by a disconnected ended up without the True and for me it was all right! kk Thanks for noticing this, now it’s working!

1 answer

1


Good afternoon, there is a very simple way to solve just use the second parameter of json_decode

$data = json_decode(file_get_contents('php://input'), true);

This happens because in theory json_decode will return any object, using true as the second parameter it is already converted to array.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.