Php how to put the keys

0

Can someone fix me how to insert the separation keys of the items?

$url = "http://www.ffcv.es/ncompeticiones/server.php?action=getActa&tmp=2017/2018&jor=3&cmp=48&idl=0201073211&idv=0201128231&id=2127995";

    $data = file_get_contents($url);
    $array = json_decode($data, true);  

    $result = [];

    foreach($array as $colors => $color){       
        if($colors == "amonestacionesLocal"){
            foreach($color as $key => $value){
                foreach($value as $clave => $valor){
                    $result[]=$valor;
                }                           
            }
        }    

        if($colors == "amonestacionesVisitante"){
            foreach($color as $key => $value){
                foreach($value as $clave => $valor){
                    $result[]=$valor;
                }                           
            }
        }    

    }       

    echo json_encode($result);

That's how I get it:

[
{
"valor": "634cef1b26b8a95c7229788b610b02bbf8ba7c8b",
"idJugador": "Nombre del jugador"
},
{
"valor": "Montesinos Piedra, Jaime",
"idJugador": "Nombre del jugador"
},
{
"valor": "amarilla",
"idJugador": "Nombre del jugador"
},
{
"valor": "53'",
"idJugador": "Nombre del jugador"
},.......
    
asked by Rafel C.F 31.12.2018 в 15:41
source

1 answer

1

Try creating a new array and using json_encode, something like this:

$url = "http://www.ffcv.es/ncompeticiones/server.php?action=getActa&tmp=2017/2018&jor=3&cmp=48&idl=0201073211&idv=0201128231&id=2127995";

$data = file_get_contents($url);
$array = json_decode($data, true);

$result = [];

foreach($array as $colors => $color){

    if($colors == "amonestacionesLocal"){
        foreach($color as $key => $value){
            $result[]=array(
                "idJugadorLocal"=>$value['idJugador'],
                "nombreLocal"=>$value['nombre'],
                "tarjetaLocal"=>$value['tarjeta'],
                "minutoLocal"=>$value['minuto'],
                "minuto"=>$value['minuto'],
            );
        }
    }

    if($colors == "amonestacionesVisitante"){
        foreach($color as $key => $value){
            $result[]=array(
                "idJugadorVisi"=>$value['idJugador'],
                "nombreVisi"=>$value['nombre'],
                "tarjetaVisi"=>$value['tarjeta'],
                "minutoVisi"=>$value['minuto'],
                "minuto"=>$value['minuto'],
            );
        }
    }
}

usort($result, function($a, $b) {
    return $a['minuto'] - $b['minuto'];
});

echo json_encode($result);
    
answered by 31.12.2018 / 16:57
source