I'm consuming data returned from a JSON Web Service , where I run arrays , to compare values between URLs and < strong> if equal , replace with its name. We have teams, places, categories and modal names, each with its identification code, eg:
Return of one of Team URL results using var_dump:
array(1241) {
[0]=> array(5) {
["codigo"]=> string(1) "4"
["modalidade"]=> string(1) "2"
["categoria"]=> string(1) "4"
["nome"]=> string(8) "Arbos SA"
["abreviado"]=> string(3) "ARB"
}
The string of the Teams URL is matched with the string from > URL results , and if the two are the same, it replaces the return by the team name, and the same rule applies to the other URLs.
I created a script using PHP to filter them and jQuery to start the tables, the script works, but sometimes I would return some errors like:
Illegal string offset
Warning: Invalid argument supplied for foreach
I've been studying here in the php forum and documentation and found solutions like is_array, isset and empty, I tried to apply them correctly, but as your layperson in php I think it's not correct , and even after touching the code so much, began to return:
Uncaught SyntaxError: Unexpected token
In the chrome console, to what I researched, some of the keys are closed in the wrong way, I already looked for the error and did not find where ... I desperately ask for help, someone who can explain the applications and syntax so I can run the code without errors, thank you in advance for your time and attention!
Ex URL:
$api_resultados = 'https://sportsmanager.com.br/api/[email protected]&token=SXLSO8342HSDE78623GVS7234GNMSKL&inicio=2018-'. $dm .'-01&fim='. $dt .'-12-31';
$resultados = wp_remote_get( $api_resultados );
$resultado = json_decode( wp_remote_retrieve_body( $resultados), true);
Errors occur between these lines, sometimes in the array_column with the array_search, sometimes in the foreach:
<?php
if(!empty($campeonato) && !is_array($campeonato) && !is_array($resultado) && !is_array($equipe) && !is_array($local) && !is_array($categoria) && !is_array($modalidade)){
print_r('Não encontrado');
}else{
$col = array_column($equipe, 'codigo');
$loc = array_column($local, 'codigo');
$cat = array_column($categoria, 'codigo');
$mod = array_column($modalidade, 'codigo');
foreach($campeonato as $camp){
if(!is_array($camp)){
}else{
foreach($resultado as $result){
if(!is_array($result)){
}else{
if(isset($result['mandante']) && isset($result['visitante']) && isset($result['local']) && isset($result['data'])) {
$key4 = array_search($camp['categoria'], $cat);
$categorizacao = '';
if($key4){
$categorizacao = $categoria[$key4]['nome'];
}
$key5 = array_search($camp['modalidade'], $mod);
$modalidadez = '';
if($key5){
$modalidadez = $modalidade[$key5]['nome'];
}
$key1 = array_search($result['mandante'], $col);
$mandante = '';
if($key1){
$mandante = $equipe[$key1]['nome'];
}
$key2 = array_search($result['visitante'], $col);
$visitante = '';
if($key2){
$visitante = $equipe[$key2]['nome'];
}
$key3 = array_search($result['local'], $loc);
$localizacao = '';
if($key3){
$localizacao = $local[$key3]['nome'];
}
$data = $result['data'];
$data = date('d/m/Y', strtotime($data));
$i = 0;
if(isset($camp['codigo']) && isset($result['campeonato']) && $camp['codigo'] == $result['campeonato']){
$retorno_resultados[++$i][] = array(
'mandante' => $result['mandante'],
'visitante' => $result['visitante'],
'nm_mandante' => $mandante,
'nm_visitante' => $visitante,
'nm_cat' => $categorizacao,
'nm_mod' => $modalidadez,
'nm_local' => $localizacao,
'id' => $camp['codigo'],
'modalidade' => $camp['modalidade'],
'categoria' => $camp['categoria'],
'data' => $result['data'],
'data_certa' => $data,
'placar1n' => $result['placar1n'],
'placar2n' => $result['placar2n'],
'placar1p' => $result['placar1p'],
'placar2p' => $result['placar2p'],
'placar1s' => $result['placar1s'],
'placar2s' => $result['placar2s'],
'jogo' => $result['jogo'],
'horario' => $result['horario'],
'jogo' => $result['codigo'],
);
};
};
};
};
};
};
};
<script>
$(function() {
<?php foreach($retorno_resultados as $val){ ?>
var html3 = '';
var id='';
<?php foreach($val as $res){ ?>
html3 += '<tr><td><?php echo $res['data_certa'];?>  às  <?php echo $res['horario'];?></td><td><?php echo $res['nm_local'];?></td><td><?php echo $res['nm_mandante'];?></td><td><?php echo $res['nm_visitante'];?></td><td><?php echo $res['nm_mod']?> <?php echo $res['nm_cat']?></td></tr>';
id = <?php echo $res['id'] ?>;
<?php } ?>
$('#equipesTbody').html(html3);
<?php } ?>
});
I'm online all the time if you need more information, or if you want to chat, I can also pass the URLs to compare the values for the explanation, a big hug!