Created
January 11, 2019 10:05
-
-
Save lolocoo/6a71542c027728a4c3de2d756f71600f to your computer and use it in GitHub Desktop.
ranking.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$arr = [ | |
['dmid' => 12, 'num' => 1], | |
['dmid' => 13, 'num' => 12], | |
['dmid' => 14, 'num' => 12], | |
['dmid' => 15, 'num' => 6], | |
['dmid' => 16, 'num' => 8], | |
['dmid' => 17, 'num' => 8], | |
['dmid' => 18, 'num' => 0], | |
['dmid' => 19, 'num' => 0], | |
['dmid' => 20, 'num' => 0], | |
]; | |
$arr = array_reduce($arr, function($pre, $cur){ | |
if (empty($pre[$cur['num']])) { | |
$pre[$cur['num']] = []; | |
} | |
array_push($pre[$cur['num']], $cur); | |
return $pre; | |
}, []); | |
krsort($arr); | |
$arr = array_values($arr); | |
$arr = array_map(function($item, $index){ | |
return array_map(function($sub) use($index) { | |
return array_merge($sub, ['place' => $index + 1]); | |
}, $item); | |
}, $arr, array_keys($arr)); | |
$arr = array_reduce($arr, function($pre, $cur){ | |
$pre = array_merge($pre, $cur); | |
return $pre; | |
}, []); | |
echo json_encode($arr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment