We help IT Professionals succeed at work.

Could you suggest the beter way to convert this json structure to a PHP array?

Eduardo Fuerte
on
Hi Experts!

Could you suggest the beter way to convert this json structure to a PHP array?

Sample: 
{
  "numeroGuia": "sample string 1",
  "paciente": {
    "nome": "sample string 1",
    "endereco": {
      "cep": "sample string 1",
      "logradouro": "sample string 2",
      "numero": 3,
      "complemento": "sample string 4",
      "bairro": "sample string 5",
      "cidade": "sample string 6"
    },
    "dataNascimento": "2018-10-22T17:53:58.4603585-03:00",
    "cpf": "sample string 2",
    "telefone": "sample string 3",
    "email": "sample string 4",
    "sexo": "sample string 5",
    "peso": 1.1,
    "convenio": 1,
    "plano": "sample string 6",
    "matricula": "sample string 7"
  },
  "contratante": {
    "tipo": 64,
    "contrato": 1
  },
  "atendimento": {
    "diagnostico": "sample string 1",
    "recurso": "sample string 2",
    "exames": "sample string 3",
    "carater": 64,
    "motivoAtendimento": "sample string 4",
    "cid": "sample string 5",
    "produto": "sample string 6"
  },
  "captacao": {
    "cor": "sample string 1",
    "dataInicio": "2018-10-22T17:53:58.4759589-03:00",
    "dataFim": "2018-10-22T17:53:58.4759589-03:00"
  },
  "origem": {
    "dataAgendadaChegadaOrigem": "2018-10-22T17:53:58.4759589-03:00",
    "nomeLocal": "sample string 1",
    "endereco": {
      "cep": "sample string 1",
      "logradouro": "sample string 2",
      "numero": 3,
      "complemento": "sample string 4",
      "bairro": "sample string 5",
      "cidade": "sample string 6"
    },
    "telefone": "sample string 2",
    "responsavel": "sample string 3"
  },
  "destino": {
    "dataAgendadaChegadaDestino": "2018-10-22T17:53:58.4759589-03:00",
    "nomeLocal": "sample string 1",
    "endereco": {
      "cep": "sample string 1",
      "logradouro": "sample string 2",
      "numero": 3,
      "complemento": "sample string 4",
      "bairro": "sample string 5",
      "cidade": "sample string 6"
    },
    "telefone": "sample string 2",
    "responsavel": "sample string 3"
  }
}

Open in new window


Thanks in advance!
Comment
Watch Question

David FavorFractional CTO
Distinguished Expert 2019

Commented:
Unsure exactly what you're asking.

Expand your question to clarify the exact, current problem you're having with your current JSON structure.

You have many nested structures, so likely best to keep this JSON, rather than trying to flatten your data into a simple array.
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
I don't understand the question - better how?

You can convert from JSON to a PHP structure with json_decode()

$data = json_decode($json);
// WHERE $json HOLDS THE JSON string

Open in new window


After that you would access it as you would any object / array combination

echo $data->paciente->endereco->complemento;

Open in new window

Will output
sample string 4

Open in new window

Eduardo FuerteDeveloper and Analyst

Author

Commented:
Hi Julian

better how?

Maybe it's the only way available...

Thanks for help!