0

I have this JSON

{
    "code": "1",
    "message": "User Created",
    "data": {
        "name": "Customer Name",
        "email": "customer@email.com"
    }
}

How do I get the name and email here using PHP?

Mr X
  • 139
  • 2
  • 15
n00b
  • 192
  • 13

1 Answers1

2
$json_str = '{
    "code": "1",
    "message": "User Created",
    "data": {
        "name": "Customer Name",
        "email": "customer@email.com"
    }
}';

$arr = json_decode($json_str, true);
echo $arr['data']['name'];
echo $arr['data']['email'];
Kita
  • 2,585
  • 18
  • 25
  • 1
    Might want to use $array instead of $obj to avoid confusion since you made an associative array with the second argument to json_decode set to true. – Devon Jun 07 '18 at 13:07
  • @Devon good point thanks, i'll update the code – Kita Jun 07 '18 at 13:08