fork download
  1. <?php
  2. $json = '[{"_id":"4940dgfd95jfsd9","score":15,"user":0,"type":"xxx"},
  3.  
  4. {"_id":"4940dsdsvhjttt89","score":4,"user":0,"type":"yyy"},
  5. {"_id":"4941dgfdsdfggd9","score":7,"user":0,"type":"yyy"},
  6. {"_id":"4941dfds43rfsff9","score":21,"user":1,"type":"yyy"},
  7.  
  8. {"_id":"4941dgfd93jfsd0","score":15,"user":1,"type":"xxx"},
  9.  
  10. {"_id":"4942dgdsadvfd9","score":8,"user":1,"type":"yyy"},
  11. {"_id":"4942dgfdaefcod8","score":4,"user":2,"type":"yyy"},
  12. {"_id":"4942dgfdsarfsd1","score":32,"user":2,"type":"yyy"},
  13.  
  14. {"_id":"4943dgfddasjfsd5","score":20,"user":2,"type":"xxx"},
  15. {"_id":"4943dasrtqxrfsd9","score":15,"user":3,"type":"xxx"},
  16.  
  17. {"_id":"4943dgfooorafsd8","score":15,"user":3,"type":"yyy"},
  18. {"_id":"4944dgfddadfsd3","score":15,"user":3,"type":"yyy"},
  19.  
  20. {"_id":"4944dgfd9dssdc4","score":15,"user":4,"type":"xxx"},
  21.  
  22. {"_id":"4944dgdacbbrttt9","score":19,"user":4,"type":"yyy"},
  23.  
  24. {"_id":"4945dgfsaddddd5","score":15,"user":4,"type":"xxx"}]';
  25.  
  26. //var_dump(json_decode($json), true);
  27.  
  28.  
  29. $jsonArray = json_decode($json, true);
  30.  
  31. function returnJson($jsonArray) {
  32.  
  33. //var_dump($jsonArray[0]["score"]);
  34. $totalScore1 = [];
  35. $totalScore2 = [];
  36. $totalScore3 = [];
  37. $totalScore4 = [];
  38. $totalScore5 = [];
  39.  
  40. foreach ($jsonArray as $value){
  41.  
  42.  
  43. if ($value["type"] == "yyy") {
  44. $number = $value["user"];
  45. $score = $value['score'];
  46.  
  47.  
  48. switch($number){
  49. case 0:
  50. array_push($totalScore1, ["_id" => $value["_id"], "score" => $score, "user" => $number, "type" => $value["type"]]);
  51.  
  52. break;
  53. case 1:
  54. array_push($totalScore2, ["_id" => $value["_id"], "score" => $score, "user" => $number, "type" => $value["type"]]);
  55. break;
  56. case 2:
  57. array_push($totalScore3, ["_id" => $value["_id"], "score" => $score, "user" => $number, "type" => $value["type"]]);
  58.  
  59. break;
  60. case 3:
  61. array_push($totalScore4, ["_id" => $value["_id"], "score" => $score, "user" => $number, "type" => $value["type"]]);
  62.  
  63. break;
  64. case 4:
  65. array_push($totalScore5, ["_id" => $value["_id"], "score" => $score, "user" => $number, "type" => $value["type"]]);
  66.  
  67. break;
  68. }
  69. }
  70. }
  71. $resultArray = array_merge(checkArray($totalScore1),
  72. checkArray($totalScore2),
  73. checkArray($totalScore3),
  74. checkArray($totalScore4),
  75. checkArray($totalScore5)
  76. );
  77. $resultJson = json_encode($resultArray);
  78.  
  79. var_dump($resultJson);
  80.  
  81. }
  82. function checkArray($totalScore){
  83.  
  84.  
  85.  
  86. if(count($totalScore) > 1){
  87. //array_unique($totalScore);
  88.  
  89. foreach ($totalScore as $key => $value){
  90. $min = min($value);
  91. //var_dump($totalScore);
  92.  
  93. if($value == $min){
  94. //var_dump($value);
  95. unset ($totalScore[$key]) ;
  96.  
  97. }
  98. }
  99. return $totalScore;
  100. }
  101. return $totalScore;
  102. }
  103. returnJson($jsonArray);
  104.  
Success #stdin #stdout 0.04s 52480KB
stdin
Standard input is empty
stdout
string(531) "[{"_id":"4940dsdsvhjttt89","score":4,"user":0,"type":"yyy"},{"_id":"4941dgfdsdfggd9","score":7,"user":0,"type":"yyy"},{"_id":"4941dfds43rfsff9","score":21,"user":1,"type":"yyy"},{"_id":"4942dgdsadvfd9","score":8,"user":1,"type":"yyy"},{"_id":"4942dgfdaefcod8","score":4,"user":2,"type":"yyy"},{"_id":"4942dgfdsarfsd1","score":32,"user":2,"type":"yyy"},{"_id":"4943dgfooorafsd8","score":15,"user":3,"type":"yyy"},{"_id":"4944dgfddadfsd3","score":15,"user":3,"type":"yyy"},{"_id":"4944dgdacbbrttt9","score":19,"user":4,"type":"yyy"}]"