2016-05-27 5 views
0

配列のソートに問題があります。私はjsonと一緒に私がksortでソートした配列を返そうとしています。配列がうまくソートされているように見えますが、私がjsonで返すと、実際の状態になります。ここで配列のソートに関する問題、json_encode

はコードです:

$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); 
} 
?> 
+0

私はksortとJSONという言葉を使用しています。javascriptでソートされていないと仮定していますか? http://stackoverflow.com/questions/5199901/how-to-sort-an-associative-array-by-its-values-in-javascriptなぜ2つの引数をksortに渡していますか? 2番目(オプション)の引数は "sort_flags"にする必要があります –

+1

[Little Bobby](http://bobby-tables.com/)は[あなたのスクリプトはSQLインジェクション攻撃の危険にさらされていると言います。](http://stackoverflow.com/質問/ 60174/how-can-i-prevent-sql-injection-in-php)を使用します。 [文字列をエスケープする](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)でも安全ではありません! –

+1

[mysql_ *関数の使用をやめてください](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)。 [これらの拡張機能](http://php.net/manual/en/migration70.removed-exts-sapis.php)はPHP 7で削除されました。[prepared](http://en.wikipedia.org/ [PDO](http://php.net/manual/en/pdo.prepared-statements.php)および[MySQLi](http://php.net/manual/en/mysqli.quickstart)のwiki/Prepared_statement)ステートメント.prepared-statements.php)、PDOの使用を検討してください。[これは本当に簡単です](http://jayblanchard.net/demystifying_php_pdo.html)。 –

答えて

0

は、それが完璧に動作し、このいずれかの関数ksortを変更する問題を修正しました。

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)); 
関連する問題