I'm having issues with sorting an array. I'm trying to return with json the array that I've sorted with ksort, looks like the array is sorted well but when I return it with json it becomes to the real state.
Here is the code:
$sql = mysql_query("select e.id_equip,e.nom from Equips e inner join Competicions c on e.id_competicio=c.id_competicio where c.nom='Preferent' and c.actiu=1");
$tempArray = array();
$json = array();
while($row = mysql_fetch_assoc($sql)){
$sqlDadesLocal = mysql_query("Select SUM(case when resultat_total_local >= 4 then 1 else 0 end) as partitsGuanyats, SUM(case when resultat_total_local < 4 then 1 else 0 end) as partitsPerduts, SUM(resultat_parcial_local) as jocsAFavor, SUM(resultat_parcial_visitant) as jocsEnContra from Actes where id_equip_local = '".$row['id_equip']."'");
$DadesLocal = MySQL_fetch_row($sqlDadesLocal);
$sqlDadesVisitant = mysql_query("Select SUM(case when resultat_total_visitant >= 4 then 1 else 0 end) as partitsGuanyats, SUM(case when resultat_total_visitant < 4 then 1 else 0 end) as partitsPerduts, SUM(resultat_parcial_visitant) as jocsAFavor, SUM(resultat_parcial_local) as jocsEnContra from Actes where id_equip_visitant = '".$row['id_equip']."'");
$DadesVisitant = MySQL_fetch_row($sqlDadesVisitant);
$SumaPartitsGuanyats = $DadesLocal[0]+$DadesVisitant[0];
$SumaPartitsPerduts = $DadesLocal[1]+$DadesVisitant[1];
$SumaJocsAFavor = $DadesLocal[2]+$DadesVisitant[2];
$SumaJocsEnContra = $DadesLocal[3]+$DadesVisitant[3];
$DiferenciaJocs = $SumaJocsAFavor-$SumaJocsEnContra;
$Punts = $SumaPartitsGuanyats*2;
$json['nomEquip'] = $row['nom'];
$json['partitsGuanyats'] = $SumaPartitsGuanyats;
$json['partitsPerduts'] = $SumaPartitsPerduts;
$json['jocsAFavor'] = $SumaJocsAFavor;
$json['jocsEnContra'] = $SumaJocsEnContra;
$json['diferenciaJocs'] = $DiferenciaJocs;
$json['puntsTotals'] = $Punts;
$tempArray[]= $json;
}
ksort($tempArray,$tempArray['puntsTotals']);
echo json_encode(array('preferent'=>$tempArray));
mysql_close($conn);
}
?>