-3

im trying to do this json decoding my professor suggested i try, but i cant seem to pass the address part. Any ideas?

{"fname": "Nick", "mname": "F.", "lname": "Delos Reyes", "birthday": "1995-04-01", "address": { "Country": "America", "state": "New York" }}

Expected output:

Name : Nick F. Delos Reyes
Birthday : 1995-04-01 
Address  :Caramoran, New York
ADyson
  • 57,178
  • 14
  • 51
  • 63
  • What have you tried, precisely? Always shoe your code. This will show you the general approach: [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – ADyson Jul 22 '21 at 05:21
  • 1
    P.s. "Caramoran" doesn't appear anywhere in the JSON so it's unclear how you expect that to be in the output. – ADyson Jul 22 '21 at 05:23

1 Answers1

0

There are many ways to it. Following is an example to parse the name.

$array = json_decode('{"fname": "Nick", "mname": "F.", "lname": "Delos Reyes", "birthday": "1995-04-01", "address": { "Country": "America", "state": "New York" }}', true);
$name = [$array['fname'], $array['mname'], $array['lname']];
$outputName = 'Name:' . implode(' ', $name);

It uses json_decode() to convert the JSON to an associative array. It then put the name parts in an array. It uses implode() to join the name parts into a string.

kiatng
  • 2,847
  • 32
  • 39