Failed to Replace ID with PHP Team Name

Asked

Viewed 48 times

1

I am consuming a web service in php, I created a function to compare championships and return me the id of the teams that will play or will play, in another url, I have the name of the teams and the ids of them, I’m trying to compare the id of the teams that are in the games with the URL teams and replace the ID by the name, but it’s not working, they would tell me where I’m going wrong?

Urls

<?php
// pega os valores da primeira URL
$api_campeonatos = 'https://sportsmanager.com.br/api/[email protected]&token=SXLSO8342HSDE78623GVS7234GNMSKL&ano=2018&status=A';
$campeonatos = wp_remote_get( $api_campeonatos );
$campeonato = json_decode( wp_remote_retrieve_body( $campeonatos ), true );

// pega os valores da segunda URL
$api_resultados = 'https://sportsmanager.com.br/api/[email protected]&token=SXLSO8342HSDE78623GVS7234GNMSKL&ano=2018&status=A';
$resultados = wp_remote_get( $api_resultados );
$resultado = json_decode( wp_remote_retrieve_body( $resultados ), true );

$api_equipes = 'https://sportsmanager.com.br/api/[email protected]&token=SXLSO8342HSDE78623GVS7234GNMSKL';
$equipes = wp_remote_get( $api_equipes );
$equipe = json_decode( wp_remote_retrieve_body( $equipes ), true );

$retorno_resultados = array();

Filters teams from a specific championship game:

foreach($campeonato as $camp){
    foreach($resultado as $result){
        if(!is_array($result)){

        }else{
            if(isset($camp['codigo']) && isset($result['campeonato']) && $camp['codigo'] == $result['campeonato']){
                $retorno_resultados[$result['campeonato']][] = array(
                    'mandante' => $result['mandante'], 
                    'visitante' => $result['visitante'],
                    'nm_mandante' => '',
                    'nm_visitante' => '',
                    'id' => $camp['codigo'],
                    'data' => $result['data'],
                    'placar1n' => $result['placar1n'],
                    'placar2n' => $result['placar2n'],
                    'placar1p' => $result['placar1p'],
                    'placar2p' => $result['placar2p'],
                    'placar1s' => $result['placar1s'],
                    'placar2s' => $result['placar2s']
                ); // <--- adiciona
            }
        }  
    }
}

I replaced the ID with the name by searching the Urls "RESULTS" and "TEAMS":

foreach($retorno_resultados as $a){
    foreach($a as $result){
        $mandante = '';
        $visitante = '';
        foreach($equipe as $role){
            if(!is_array($role)){
                    //$retorno = $retorno.'<td>'.$row.'</td>';
            }else{
                if($role['codigo'] == $result['mandante']){
                    //$retorno_equipes[] = array('mandante' => '<td>'.$role['nome'].'</td>', 'visitante' => $role['nome']); // <--- adiciona
                    $result['nm_mandante'] = $role['nome'];
                    //echo $result['nm_mandante'];
                }

                if($role['codigo'] == $result['visitante']){
                    $result['nm_visitante'] = $role['nome'];
                    //echo $role['nome'];
                }
            }   
        }
        //echo 'chegou';
        //$result['nm_mandante'] = $mandante;
        //$result['nm_visitante'] = $visitante;
    }
}

Print the content in the div:

<script>

$(function(){

    <?php foreach($retorno_resultados as $val){ ?>

    var html3 = '';
    var id='';

    <?php foreach($val as $res){ ?>

    html3 += '<tr><td><?php echo $res['nm_mandante'];?>x<?php echo $res['nm_visitante'];?></td></tr>';
    id = <?php echo $res['id'] ?>;


    <?php } ?>
    $('#cont-'+id).html(html3);
    <?php } ?>

});

</script>

JSON return URL:results:

{
"codigo": "31",
"campeonato": "18",
"data": "2018-09-11 00:00:00",
"horario": "19H",
"local": "2",
"realizada": "N",
"jogo": "0",
"fase": "2",
"rodada": "0",
"mandante": "5",
"visitante": "3",
"placar1n": null,
"placar2n": null,
"placar1p": null,
"placar2p": null,
"placar1s": null,
"placar2s": null,
"grupo1": "",
"grupo2": "",
"quadra": "2",
"obs": null
},
  {
"codigo": "32",
"campeonato": "18",
"data": "2018-09-25 00:00:00",
"horario": "19H",
"local": "4",
"realizada": "N",
"jogo": "0",
"fase": "2",
"rodada": "0",
"mandante": "7",
"visitante": "3",
"placar1n": null,
"placar2n": null,
"placar1p": null,
"placar2p": null,
"placar1s": null,
"placar2s": null,
"grupo1": "",
"grupo2": "",
"quadra": "4",
"obs": null
},
  {
"codigo": "25",
"campeonato": "19",
"data": "2018-08-13 00:00:00",
"horario": "19h",
"local": "9",
"realizada": "N",
"jogo": "0",
"fase": "2",
"rodada": "0",
"mandante": "2",
"visitante": "6",
"placar1n": null,
"placar2n": null,
"placar1p": null,
"placar2p": null,
"placar1s": null,
"placar2s": null,
"grupo1": "",
"grupo2": "",
"quadra": "9",
"obs": null
},

Return JSON URL:teams:

 {
"codigo": "1",
"nome": "Arbos Santo André",
"nomeabreviado": "ARBOS SA",
"url": "arbos-santo-andre",
"telefone": "",
"email": "",
"endereco": "",
"complemento": null,
"bairro": "",
"cidade": "",
"uf": "",
"cep": ""
},
  {
"codigo": "2",
"nome": "Arbos São Bernardo do Campo",
"nomeabreviado": "ARBOS SBC",
"url": "arbos-sao-bernardo-do-campo",
"telefone": "",
"email": "",
"endereco": "",
"complemento": null,
"bairro": "",
"cidade": "",
"uf": "",
"cep": ""
},
  {
"codigo": "16",
"nome": "Arbos São Caetano do Sul",
"nomeabreviado": "ARBOS SCS",
"url": "arbos-sao-caetano-do-sul",
"telefone": "",
"email": "",
"endereco": "",
"complemento": null,
"bairro": "",
"cidade": "",
"uf": "",
"cep": ""
},
  {
"codigo": "23",
"nome": "Barão - Mauá",
"nomeabreviado": "B. MAUÁ",
"url": "barao--maua",
"telefone": "",
"email": "",
"endereco": "rua onze de junho, 166",
"complemento": null,
"bairro": "jd do pilar",
"cidade": "maua",
"uf": "SP",
"cep": "09360-010"
},
  {
"codigo": "5",
"nome": "Barão SBC",
"nomeabreviado": "BAR",
"url": "barao-sbc",
"telefone": "",
"email": "",
"endereco": "",
"complemento": null,
"bairro": "",
"cidade": "",
"uf": "",
"cep": ""
},

NOTE: THERE ARE MORE RETURN RECORDS, BUT IT IS VERY EXTENSIVE.

  • Which error is shown?

  • it does not return error, it is only bringing an empty return by what seems

  • Place the result of the Apis

1 answer

1


Instead of creating another foreach just to search the names of the teams, it’s easier to do this at the time you’re creating the array, using two php features. Who are the array_search to search a value and obtain the key, and array_column to bring a list of values from a given column of the array.

Your code would look like this:

$colunas =  array_column($equipe, 'codigo'); // <-- pega todos os valores da coluna 'codigo'

foreach($campeonato as $camp){
    foreach($resultado as $result){
        if(!is_array($result)){

        }else{


            // pesquisa chave do mandante
            $key1 = array_search($result['mandante'],$colunas);
            $mandante = $equipe[$key1]['nome']; // <-- pega o valor

            // pesquisa chave do visitante
            $key2 = array_search($result['visitante'], $colunas);
            $visitante = $equipe[$key2]['nome']; // <-- pega o valor

            if(isset($camp['codigo']) && isset($result['campeonato']) && $camp['codigo'] == $result['campeonato']){
                $retorno_resultados[$result['campeonato']][] = array(
                    'mandante' => $result['mandante'], 
                    'visitante' => $result['visitante'],
                    'nm_mandante' => $mandante,
                    'nm_visitante' => $visitante,
                    'id' => $camp['codigo'],
                    'data' => $result['data'],
                    'placar1n' => $result['placar1n'],
                    'placar2n' => $result['placar2n'],
                    'placar1p' => $result['placar1p'],
                    'placar2p' => $result['placar2p'],
                    'placar1s' => $result['placar1s'],
                    'placar2s' => $result['placar2s']
                ); // <--- adiciona
            }
        }  
    }
}
  • values still return blank, I need to change something in jQuery call?

  • it worked!!! I switched the array in jquery.

  • Thank you so much again man! you are show

  • That’s it! Then you use it in jquery!

  • @Miguelcampos no problem!

  • @Miguelcampos I made an edition that will make your code more performance. I advise you to use this way is now.

  • perfect Andrei!

  • I sent you an e-mail with a question

Show 3 more comments

Browser other questions tagged

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