<?php
$json = '[{"_id":"4940dgfd95jfsd9","score":15,"user":0,"type":"xxx"},
{"_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":"4941dgfd93jfsd0","score":15,"user":1,"type":"xxx"},
{"_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":"4943dgfddasjfsd5","score":20,"user":2,"type":"xxx"},
{"_id":"4943dasrtqxrfsd9","score":15,"user":3,"type":"xxx"},
{"_id":"4943dgfooorafsd8","score":15,"user":3,"type":"yyy"},
{"_id":"4944dgfddadfsd3","score":15,"user":3,"type":"yyy"},
{"_id":"4944dgfd9dssdc4","score":15,"user":4,"type":"xxx"},
{"_id":"4944dgdacbbrttt9","score":19,"user":4,"type":"yyy"},
{"_id":"4945dgfsaddddd5","score":15,"user":4,"type":"xxx"}]';
//var_dump(json_decode($json), true);
function returnJson($jsonArray) {
//var_dump($jsonArray[0]["score"]);
$totalScore1 = [];
$totalScore2 = [];
$totalScore3 = [];
$totalScore4 = [];
$totalScore5 = [];
foreach ($jsonArray as $value){
if ($value["type"] == "yyy") {
$number = $value["user"];
$score = $value['score'];
switch($number){
case 0:
array_push($totalScore1, ["_id" => $value["_id"], "score" => $score, "user" => $number, "type" => $value["type"]]);
break;
case 1:
array_push($totalScore2, ["_id" => $value["_id"], "score" => $score, "user" => $number, "type" => $value["type"]]); break;
case 2:
array_push($totalScore3, ["_id" => $value["_id"], "score" => $score, "user" => $number, "type" => $value["type"]]);
break;
case 3:
array_push($totalScore4, ["_id" => $value["_id"], "score" => $score, "user" => $number, "type" => $value["type"]]);
break;
case 4:
array_push($totalScore5, ["_id" => $value["_id"], "score" => $score, "user" => $number, "type" => $value["type"]]);
break;
}
}
}
checkArray($totalScore2),
checkArray($totalScore3),
checkArray($totalScore4),
checkArray($totalScore5)
);
}
function checkArray($totalScore){
if(count($totalScore) > 1){ //array_unique($totalScore);
foreach ($totalScore as $key => $value){
//var_dump($totalScore);
if($value == $min){
//var_dump($value);
unset ($totalScore[$key]) ;
}
}
return $totalScore;
}
return $totalScore;
}
returnJson($jsonArray);
PD9waHAKJGpzb24gPSAnW3siX2lkIjoiNDk0MGRnZmQ5NWpmc2Q5Iiwic2NvcmUiOjE1LCJ1c2VyIjowLCJ0eXBlIjoieHh4In0sCiAgICAKICB7Il9pZCI6IjQ5NDBkc2RzdmhqdHR0ODkiLCJzY29yZSI6NCwidXNlciI6MCwidHlwZSI6Inl5eSJ9LAogIHsiX2lkIjoiNDk0MWRnZmRzZGZnZ2Q5Iiwic2NvcmUiOjcsInVzZXIiOjAsInR5cGUiOiJ5eXkifSwKICB7Il9pZCI6IjQ5NDFkZmRzNDNyZnNmZjkiLCJzY29yZSI6MjEsInVzZXIiOjEsInR5cGUiOiJ5eXkifSwKICAKICB7Il9pZCI6IjQ5NDFkZ2ZkOTNqZnNkMCIsInNjb3JlIjoxNSwidXNlciI6MSwidHlwZSI6Inh4eCJ9LAogIAogIHsiX2lkIjoiNDk0MmRnZHNhZHZmZDkiLCJzY29yZSI6OCwidXNlciI6MSwidHlwZSI6Inl5eSJ9LAogIHsiX2lkIjoiNDk0MmRnZmRhZWZjb2Q4Iiwic2NvcmUiOjQsInVzZXIiOjIsInR5cGUiOiJ5eXkifSwKICB7Il9pZCI6IjQ5NDJkZ2Zkc2FyZnNkMSIsInNjb3JlIjozMiwidXNlciI6MiwidHlwZSI6Inl5eSJ9LAogIAogIHsiX2lkIjoiNDk0M2RnZmRkYXNqZnNkNSIsInNjb3JlIjoyMCwidXNlciI6MiwidHlwZSI6Inh4eCJ9LAogIHsiX2lkIjoiNDk0M2Rhc3J0cXhyZnNkOSIsInNjb3JlIjoxNSwidXNlciI6MywidHlwZSI6Inh4eC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gICBhcnJheV9wdXNoKCR0b3RhbFNjb3JlMSwgWyJfaWQiID0+ICR2YWx1ZVsiX2lkIl0sICJzY29yZSIgPT4gJHNjb3JlLCAidXNlciIgPT4gJG51bWJlciwgInR5cGUiID0+ICR2YWx1ZVsidHlwZSJdXSk7CiAgICAgICAgICAgIAogICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgY2FzZSAxOgogICAgICAgICAgICBhcnJheV9wdXNoKCR0b3RhbFNjb3JlMiwgWyJfaWQiID0+ICR2YWx1ZVsiX2lkIl0sICJzY29yZSIgPT4gJHNjb3JlLCAidXNlciIgPT4gJG51bWJlciwgInR5cGUiID0+ICR2YWx1ZVsidHlwZSJdXSk7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICBjYXNlIDI6CiAgICAgICAgICAgIGFycmF5X3B1c2goJHRvdGFsU2NvcmUzLCBbIl9pZCIgPT4gJHZhbHVlWyJfaWQiXSwgInNjb3JlIiA9PiAkc2NvcmUsICJ1c2VyIiA9PiAkbnVtYmVyLCAidHlwZSIgPT4gJHZhbHVlWyJ0eXBlIl1dKTsKICAgICAgICAgICAgICAgIAogICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgY2FzZSAzOgogICAgICAgICAgICBhcnJheV9wdXNoKCR0b3RhbFNjb3JlNCwgWyJfaWQiID0+ICR2YWx1ZVsiX2lkIl0sICJzY29yZSIgPT4gJHNjb3JlLCAidXNlciIgPT4gJG51bWJlciwgInR5cGUiID0+ICR2YWx1ZVsidHlwZSJdXSk7CiAgICAgICAgICAgIAogICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgY2FzZSA0OiAKICAgICAgICAgICAgYXJyYXlfcHVzaCgkdG90YWxTY29yZTUsIFsiX2lkIiA9PiAkdmFsdWVbIl9pZCJdLCAic2NvcmUiID0+ICRzY29yZSwgInVzZXIiID0+ICRudW1iZXIsICJ0eXBlIiA9PiAkdmFsdWVbInR5cGUiXV0pOwogICAgICAgICAgICAKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICAkcmVzdWx0QXJyYXkgPSBhcnJheV9tZXJnZShjaGVja0FycmF5KCR0b3RhbFNjb3JlMSksCiAgICAgICAgICAgIGNoZWNrQXJyYXkoJHRvdGFsU2NvcmUyKSwKICAgICAgICAgICAgY2hlY2tBcnJheSgkdG90YWxTY29yZTMpLAogICAgICAgICAgICBjaGVja0FycmF5KCR0b3RhbFNjb3JlNCksCiAgICAgICAgICAgIGNoZWNrQXJyYXkoJHRvdGFsU2NvcmU1KQogICAgICAgICAgICApOwogICAgJHJlc3VsdEpzb24gPSBqc29uX2VuY29kZSgkcmVzdWx0QXJyYXkpOwogICAgCiAgICB2YXJfZHVtcCgkcmVzdWx0SnNvbik7CiAgICAKfQpmdW5jdGlvbiBjaGVja0FycmF5KCR0b3RhbFNjb3JlKXsKCiAgICAKICAgIAogICAgICAgIGlmKGNvdW50KCR0b3RhbFNjb3JlKSA+IDEpewogICAgICAgIC8vYXJyYXlfdW5pcXVlKCR0b3RhbFNjb3JlKTsgICAKCiAgICAgICAgZm9yZWFjaCAoJHRvdGFsU2NvcmUgYXMgJGtleSA9PiAkdmFsdWUpewogICAgICAgICAgICAkbWluID0gbWluKCR2YWx1ZSk7CiAgICAgICAgICAgIC8vdmFyX2R1bXAoJHRvdGFsU2NvcmUpOwogICAgICAgICAgICAKICAgICAgICAgICAgaWYoJHZhbHVlID09ICRtaW4peyAKICAgICAgICAgICAgICAgLy92YXJfZHVtcCgkdmFsdWUpOwogICAgICAgICAgICAgICB1bnNldCAoJHRvdGFsU2NvcmVbJGtleV0pIDsKCiAgICAgICAgICAgIH0KICAgICAgICB9IAogICAgICAgIHJldHVybiAkdG90YWxTY29yZTsKICAgIH0gICAKICAgIHJldHVybiAkdG90YWxTY29yZTsKfQpyZXR1cm5Kc29uKCRqc29uQXJyYXkpOwo=