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