Access array elements from JSON

Asked

Viewed 1,008 times

2

I’m retrieving an object and turning this object into array.

Now, I need to access some elements of this array to fill in these data in the controller (Codeigniter), before returning it as JSON.

To the object below:

{
  "contratos":[

  ],
  "id":0,
  "id_operador":0,
  "pessoa_fisica":{
    "id":0,
    "rg":"",
    "dt_nascimento":"",
    "profissao":"",
    "salario":"",
    "genero":"1"
  },
  "pessoa_juridica":{
    "id":0,
    "nome_fantasia":"",
    "inscricao_estadual":""
  },
  "nome":"Wagner Carlos de Jesus Júnior",
  "cpf_cnpj":"096.686.256-25",
  "emails":[

  ],
  "enderecos":[

  ],
  "telefones":[

  ],
  "crud":null
}

I’m using the function json_decode and getting the following return with the print_r

$objeto = $_POST['objeto'];

$objeto_decode = json_decode($objeto, true);
print_r($objeto_decode);

And the return is as below, however, I do not know how to access the element individually and fill it manually. For example, fill the element [id_operador] with the ID (Session) of the logged-in user`

Array
(
    [contratos] => Array
        (
        )

    [id] => 0
    [id_operador] => 0
    [pessoa_fisica] => Array
        (
            [id] => 0
            [rg] => 
            [dt_nascimento] => 
            [profissao] => 
            [salario] => 
            [genero] => 1
        )

    [pessoa_juridica] => Array
        (
            [id] => 0
            [nome_fantasia] => 
            [inscricao_estadual] => 
        )

    [nome] => Wagner Carlos de Jesus Júnior
    [cpf_cnpj] => 096.686.256-25
    [emails] => Array
        (
        )

    [enderecos] => Array
        (
        )

    [telefones] => Array
        (
        )

    [crud] => 
)
  • Just make the call $object_decode["id_operator"].

1 answer

2


Just refer to the array item. It would look like this:

$objeto_decode['id_operador'] = session_id();

Another example, to access id of a natural person:

$objeto_decode['pessoa_fisica']['id'] = 'Coloque a variável do ID aqui';

If use:

// Isto
$objeto_decode = json_decode($objeto);
// Em vez disso
$objeto_decode = json_decode($objeto, true);

You can access the items as objects, it would be as follows:

// Acesso ao objeto id_operador
$objeto_decode->id_operador = session_id();
// Acesso ao id da pessoa física
$objeto_decode->pessoa_fisica->id = 'Coloque a variável do ID aqui';

In particular, I prefer to use objects because they are better for reading. But, this goes from each one, see which one fits best and, good programming.

  • Perfect, worked and clarified a lot. Thanks for the answer. Now I can include the information through the controller.

  • Bruno, can you tell me how I would make Inserts in different tables, using the above object, example: $objeto_decode->id_operador = session_id(); would go to the tabela devedor already the array items pessoa_juridica would go to the table devedor_pf.

  • $objeto_decode->id_operador = $operador_id; $objeto_decode->pessoa_fisica->id = $ultimo_id; $insert_devedor = $this->devedor->salvar($objeto_decode); $insert_devedor_pf = $this->devedor->salvar_devedor_pf($objeto_decode_pf);

  • knows how I could access the items of an array, which is inside another array. Example: ['contracts']['trades']['plots']. In this case, each one would be an object and I need to access the items of the object trades.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.