Json with Array within PHP array

1

I encounter a problem.

I have an API in passing URL

{
"id": "1",
"codeReferenceAdditional": "33B",
"isActive": true,
"personType": 1,
"profileType": 3,
"accessProfile": "Administradores",
"businessName": "Movidesk",
"corporateName": "Movimentti sistemas",
"cpfCnpj": "012345678900",
"userName": "admin",
"password": null,
"role": null,
"bossId": null,
"classification": null,
"createdDate": "2014-12-17T18:00:43.3339728",
"observations": "Cadastro realizado via api de pessoas.",
"addresses": [
  {
    "addressType": "Comercial",
    "country": "Brasil",
    "postalCode": "89035200",
    "state": "Santa Catarina",
    "district": "Vila Nova",
    "street": "Rua Joinville",
    "number": "209",
    "complement": "Sala 201",
    "reference": "Próximo a FURB",
    "isDefault": true
  }
],

I'm using the following structure in php to read the information

 $json = file_get_contents("https://api");



   $cliente = json_decode($json);


  echo "id: $cliente->id</br>"; 
  echo "nome: $cliente->corporateName</br>"; 
  echo "cpf/cnpj: $cliente->cpfCnpj </br> ";
  echo "Endereço: $cliente->addresses[0]->city";

But give the following error in the browser

Notice: Array to string conversion in C:\xampp\htdocs\Maps\index.php on line 30
Endereço: Array[0]->city

I'm not able to convert this second array.

    
asked by anonymous 17.04.2017 / 22:37

1 answer

1

The problem is to use the dento array of the string.

PHP is trying to convert $cliente->addresses to string and not the complete expression $cliente->addresses[0]->city

Change to any of the following modes and should work:

echo "Endereço: " . $cliente->addresses[0]->city;

Or else:

echo "Endereço: {$cliente->addresses[0]->city} ";
    
17.04.2017 / 22:46