0

I have the following code that generates a dictionary of key/value pairs in php:

 $json = file_get_contents('php://input'); 
 $obj = json_decode($json);
 print_r(json_encode($obj));

print_r produces the following output:

{"email":"someemail@gmail.com","password":"compas"}

how do you output just the value of the email key? Why doesn't $obj["email"] work?

DCR
  • 14,737
  • 12
  • 52
  • 115
  • why are you json_encoding it? that makes it a string. `$obj = json_decode($json);` then `$email = $obj->email`. If you want an associative array, `$obj = json_decode($json, true);` then `$email = $obj['email']` – Kinglish May 30 '21 at 20:56

1 Answers1

2

To turn the json_decode to an arry use following form see manual

$myjson = '{"email":"someemail@gmail.com","password":"compas"}';
$obj = json_decode($myjson,true);
print_r($obj[email]);

returns someemail@gmail.com

nbk
  • 45,398
  • 8
  • 30
  • 47