Assign Json content to PHP variable (RESOLVED)

-5

Save Galley, I'm starting in the WebService world and I'm already faced with the following problem.

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

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

Result:

{"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);

Result:

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, ie 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 do 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 a correction in the response of the server, which was returning with quotation marks, return valid:

$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
        )

)
    
asked by anonymous 28.08.2018 / 20:45

1 answer

2

I think in your case you can use list .

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

The list will get each index (starting from 0 and will assign in the sequence in which the values are arranged within CAMPOS .

You could also access these indexes through the number:

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

Although not very clear in the question, it seems to me that you want 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 traversed with foreach .

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

See working at IDEONE

    
28.08.2018 / 20:54