Assign Json content to the PHP variable

Asked

Viewed 148 times

-5

Hail to you guys, I’m starting in the Webservice world and I’ve come across the following problem.

The TOTVS Server via Rest, returns me exactly as follows:

$resposta = enviaConteudoParaAPI($cabecalho, $conteudo, $url, $tpRequisicao);
print_r($resposta);

Upshot:

{"CAMPOS":"[\"\"A1_COD\",\"A1_NOME\",\"TIPO\"]","DADOS":"[[\"00932221\",\"PEDRO GONCALVES\",\"TITULAR\"][\"00164577\",\"JORGE ARAGAO\",\"TITULAR\"]]"}

//TRATADO
$array = json_decode($resposta, true);
    $campos = $array['CAMPOS'];
    $dados = $array['DADOS'];
    print_r($array);

Upshot:

Array ( [CAMPOS] => [""A1_COD","A1_NOME","TIPO"] [DADOS] => [["00932221","PEDRO GONCALVES","TITULAR"]["00164577","JORGE ARAGAO","TITULAR"]] ) 

It turns out that I need to assign each content to a variable, that is, I need to work with each value differently:

    $A1_COD = ['$A1_COD'];
    $A1_NOME = ['A1_NOME'];
    $TIPO = ['TIPO'];
echo "Codigo: $A1_COD, Nome: $A1_NOME, Tipo: $TIPO";

Where I need the following result:

Codigo: 00932221 , Nome: PEDRO GONCALVES, Tipo: TITULAR
Codigo: 00164577 , Nome: JORGE ARAGAO,    Tipo: TITULAR  

In other words, I need to transform this array:

 Array ([["00932221","PEDRO GONÇALVES","TITULAR"]["00164577","JORGE ARAGAO","TITULAR"]])

In this array:

Array ( [A1_COD] => 00932221 [A1_NOME] => PEDRO GONÇALVES [TIPO] => TITULAR ) 
Array ( [A1_COD] => 00164577 [A1_NOME] => JORGE ARAGAO [TIPO] => TITULAR )

SOLUTION:

I requested correction in the server response, which was returning with extra quotes, return validated:

$jsonString = '{
    "CAMPOS": ["A1_COD", "A1_NOME", "TIPO"],
    "DADOS": [
        ["33436681", "MARCOS ALAN", "REPRESENTANTE"],
        ["34007644", "MARCOS ALBERTO", "TITULAR"],
        ["67762840", "MARCOS ALVES", "TITULAR"],
        ["55178561", "MARCOS ANTONIO", "TITULAR"]
    ]
}';

$array = json_decode($jsonString, true);

foreach ($array['DADOS'] as $dados) {
    $novoArray[] = array_combine($array['CAMPOS'], $dados); 
}

echo "<pre>";
print_r($novoArray);

Array
(
    [0] => Array
        (
            [A1_COD] => 33436681
            [A1_NOME] => MARCOS ALAN
            [TIPO] => REPRESENTANTE
        )

    [1] => Array
        (
            [A1_COD] => 34007644
            [A1_NOME] => MARCOS ALBERTO
            [TIPO] => TITULAR
        )

    [2] => Array
        (
            [A1_COD] => 67762840
            [A1_NOME] => MARCOS ALVES
            [TIPO] => TITULAR
        )

    [3] => Array
        (
            [A1_COD] => 55178561
            [A1_NOME] => MARCOS ANTONIO
            [TIPO] => TITULAR
        )

)

1 answer

2

I think in your case you can use the list.

list($a1_cod, $a1_nome, $tipo) = $retorno['CAMPOS'] 

The list will take each index (starting from the 0 and shall assign the values within CAMPOS.

You could access these indexes also through the number:

 echo $retorno['CAMPOS']['A1_COD']

Although it is not very clear in the question, it seems to me that you wish to combine CAMPOS with DADOS.

If it is, to do this, you can combine array_map and array_combine.

$retorno = [

    'CAMPOS' => [ 'A1_COD','A1_NOME','TIPO'],
    'DADOS' => [
        ["00932221","PEDRO GONCALVES","TITULAR"],
        ["00164577","JORGE ARAGAO","TITULAR"]
    ]
];


$valores_combinados = array_map(function ($dados) use($retorno) {
    return array_combine($retorno['CAMPOS'], $dados);
}, $retorno['DADOS']);


print_r($valores_combinados);

The Result is:

Array
(
    [0] => Array
        (
            [A1_COD] => 00932221
            [A1_NOME] => PEDRO GONCALVES
            [TIPO] => TITULAR
        )

    [1] => Array
        (
            [A1_COD] => 00164577
            [A1_NOME] => JORGE ARAGAO
            [TIPO] => TITULAR
        )

)

Above could be traveled with foreach.

foreach($valores_combinados as $valor) {
       echo $valor['A1_COD'];
}

See working on IDEONE

  • Wallace, I’ll try to be more specific.

  • If you are going to make some consideration, edit your question and add details, but I already say that you should not let someone answer your question and then say clearly what you need.

  • Wallace, I’ve edited my doubt, see if it’s clearer.

  • @Marcossantos but the answer does exactly what you want. You tested the solution I put?

  • tested, but not compiled.

  • 2

    What do you mean "not compiled"? PHP is an interpreted language.

  • It did not run because php is not accepting the array assigned to the $return variable as a valid array, so nothing is returned.

  • @Tabs and which error message specifies?

  • William, good morning. No mistake, just the page is blank.

  • Wallace, good morning! Dude, your solution is right, what was wrong was the return I was getting from the server, was with extra quotes. Thank you very much for your support.

Show 5 more comments

Browser other questions tagged

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