0

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);
}
?>
  • I see you used the words ksort and JSON.. assuming you're not seeing them sorted in javascript? http://stackoverflow.com/questions/5199901/how-to-sort-an-associative-array-by-its-values-in-javascript why are you passing two arguments to ksort?? The second (optional) argument should be "sort_flags" – Brad Kent May 27 '16 at 17:45
  • 1
    [Little Bobby](http://bobby-tables.com/) says [your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard May 27 '16 at 17:50
  • 1
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 27 '16 at 17:50

1 Answers1

0

Solved the problem changing the function ksort for this one, it works perfectly.

usort($tempArray, function($a, $b) {
        if($a['puntsTotals']==$b['puntsTotals']) return 0;
        return $a['puntsTotals'] < $b['puntsTotals']?1:-1;
});


echo json_encode(array('preferent'=>$tempArray));