How to add an object inside stdClass() and print the json

Asked

Viewed 222 times

2

How do I add a variable or object within a json. It is thus printing my json:

{"Registros":[{"RegistroOperacaoID":"39","Tipo":"11","DataOperacao":"2019-03-27 16:29:36","AlunoID":null,"ResponsavelID":"57","CursoID":"327","MatriculaID":null,"PlanoID":"68","Processado":"0","Nome":null}

I would like to add together with this data another variable or object, that would be like this:

{"Registros":[{"RegistroOperacaoID":"39","Tipo":"11","DataOperacao":"2019-03-27 16:29:36","AlunoID":null,"ResponsavelID":"57","CursoID":"327","MatriculaID":null,"PlanoID":"68","Processado":"0","variavel":"1 registro","Nome":null}

my php code looks like this:

register.operacao.php

$sql = "SELECT R.RegistroOperacaoID, R.Tipo, R.DataOperacao, R.AlunoID, R.ResponsavelID, R.CursoID, R.MatriculaID, R.PlanoID, R.Processado, A.Nome FROM {$pfx}Registrooperacoes R LEFT JOIN {$pfx}Aluno A ON (A.AlunoID = R.AlunoID) WHERE Processado = 0";

    $qry = $database->query($sql);
    $listaOperacoes = array();
    $ctaOperacoes = 0;
    if($qry){
        while ($row = fetch($qry)) {
            $listaOperacoes[] = $row;            
            $ctaOperacoes++;           
        }
    }

    @$output = new StdClass();
    //$variavel = "$ctaOperacoes registros";        
    $output->Registros = $listaOperacoes;    

    $saida = json_encode($output, JSON_UNESCAPED_UNICODE);
    echo $saida;

1 answer

0

Hello, to add a value is simple, but you do not add directly in json, notice that in your code is creating an object and the variable $saida is receiving this converted object in json, what we need to do is to place new attribute or object inside $output before printing the json.
Where will we work on your code.

    @$output = new StdClass();
    //$variavel = "$ctaOperacoes registros";

    //Vamos trabalhar aqui

    $output->Registros = $listaOperacoes;

    $saida = json_encode($output, JSON_UNESCAPED_UNICODE);
    echo $saida;

To $listaOperacoes is an array of objects, so to make the insertion we need to have the key($key) of each item, thus foreach we will help solve the problem.
The novoAtributo (can have any name) is the attribute we are creating inside an object of $listaOperacoes. The way to place this new variable is as follows.

    foreach ($listaOperacoes as $key => $value) {
        $listaOperacoes[$key]->novoAtributo = 'Eu sou um novo valor dentro do registro';
    }

Your code should be as follows friend.

    @$output = new StdClass();
    //$variavel = "$ctaOperacoes registros";    

    foreach ($listaOperacoes as $key => $value) {
        $listaOperacoes[$key]->novoAtributo = 'Eu sou um novo valor dentro do 
    registro';
    }

    $output->Registros = $listaOperacoes;    

    $saida = json_encode($output, JSON_UNESCAPED_UNICODE);
    echo $saida;
  • 1

    Hello friend, it worked perfectly, thank you very much! perfect explanation.

  • Thank you, a good way for you to help others with your question is to signal that this answer was the one that solved your problem.

Browser other questions tagged

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