3v4l.org

run code in 300+ PHP versions simultaneously
<?php $json = <<<JSON { "mlb": { "james": { "avgTotalPoints": 80.675, "avgPointsDifferential": -0.8112500000000002, "rank": null }, "mary": { "avgTotalPoints": 114.9875, "avgPointsDifferential": 6.262500000000003, "rank": null }, "lucas": { "avgTotalPoints": 66.825, "avgPointsDifferential": -9.10625, "rank": null }, "timothy": { "avgTotalPoints": 69.2625, "avgPointsDifferential": -19.14125, "rank": null }, "samantha": { "avgTotalPoints": 91.625, "avgPointsDifferential": -36.85000000000001, "rank": null } }, "nhl": { "samantha": { "avgTotalPoints": 82.20000000000002, "avgPointsDifferential": 15.005714285714289, "rank": null }, "peter": { "avgTotalPoints": 77.65714285714284, "avgPointsDifferential": -13.31857142857143, "rank": null }, "rick": { "avgTotalPoints": 91.61428571428571, "avgPointsDifferential": 11.217142857142859, "rank": null }, "bubba": { "avgTotalPoints": 119.60000000000001, "avgPointsDifferential": 28.13333333333334, "rank": null }, "james": { "avgTotalPoints": 90.18571428571428, "avgPointsDifferential": 10.915714285714285, "rank": null } } } JSON; function sortHighestToLowest(array $arr): array { return array_map(static function (array $entries): array { uasort($entries, static function (array $entry1, array $entry2): int { return $entry2['avgTotalPoints'] <=> $entry1['avgTotalPoints']; }); $rank = 0; foreach ($entries as &$entry) { $entry['rank'] = ++$rank; } return $entries; }, $arr); } $arr = json_decode($json, true, 512, JSON_THROW_ON_ERROR); print_r(sortHighestToLowest($arr));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YXP3o
function name:  (null)
number of ops:  15
compiled vars:  !0 = $json, !1 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '%7B%0A++++%22mlb%22%3A+%7B%0A++++++++%22james%22%3A+%7B%0A++++++++++++%22avgTotalPoints%22%3A+80.675%2C%0A++++++++++++%22avgPointsDifferential%22%3A+-0.8112500000000002%2C%0A++++++++++++%22rank%22%3A+null%0A++++++++%7D%2C%0A++++++++%22mary%22%3A+%7B%0A++++++++++++%22avgTotalPoints%22%3A+114.9875%2C%0A++++++++++++%22avgPointsDifferential%22%3A+6.262500000000003%2C%0A++++++++++++%22rank%22%3A+null%0A++++++++%7D%2C%0A++++++++%22lucas%22%3A+%7B%0A++++++++++++%22avgTotalPoints%22%3A+66.825%2C%0A++++++++++++%22avgPointsDifferential%22%3A+-9.10625%2C%0A++++++++++++%22rank%22%3A+null%0A++++++++%7D%2C%0A++++++++%22timothy%22%3A+%7B%0A++++++++++++%22avgTotalPoints%22%3A+69.2625%2C%0A++++++++++++%22avgPointsDifferential%22%3A+-19.14125%2C%0A++++++++++++%22rank%22%3A+null%0A++++++++%7D%2C%0A++++++++%22samantha%22%3A+%7B%0A++++++++++++%22avgTotalPoints%22%3A+91.625%2C%0A++++++++++++%22avgPointsDifferential%22%3A+-36.85000000000001%2C%0A++++++++++++%22rank%22%3A+null%0A++++++++%7D%0A++++%7D%2C%0A++++%22nhl%22%3A+%7B%0A++++++++%22samantha%22%3A+%7B%0A++++++++++++%22avgTotalPoints%22%3A+82.20000000000002%2C%0A++++++++++++%22avgPointsDifferential%22%3A+15.005714285714289%2C%0A++++++++++++%22rank%22%3A+null%0A++++++++%7D%2C%0A++++++++%22peter%22%3A+%7B%0A++++++++++++%22avgTotalPoints%22%3A+77.65714285714284%2C%0A++++++++++++%22avgPointsDifferential%22%3A+-13.31857142857143%2C%0A++++++++++++%22rank%22%3A+null%0A++++++++%7D%2C%0A++++++++%22rick%22%3A+%7B%0A++++++++++++%22avgTotalPoints%22%3A+91.61428571428571%2C%0A++++++++++++%22avgPointsDifferential%22%3A+11.217142857142859%2C%0A++++++++++++%22rank%22%3A+null%0A++++++++%7D%2C%0A++++++++%22bubba%22%3A+%7B%0A++++++++++++%22avgTotalPoints%22%3A+119.60000000000001%2C%0A++++++++++++%22avgPointsDifferential%22%3A+28.13333333333334%2C%0A++++++++++++%22rank%22%3A+null%0A++++++++%7D%2C%0A++++++++%22james%22%3A+%7B%0A++++++++++++%22avgTotalPoints%22%3A+90.18571428571428%2C%0A++++++++++++%22avgPointsDifferential%22%3A+10.915714285714285%2C%0A++++++++++++%22rank%22%3A+null%0A++++++++%7D%0A++++%7D%0A%7D'
   78     1        INIT_FCALL                                               'json_decode'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 <true>
          4        SEND_VAL                                                 512
          5        SEND_VAL                                                 4194304
          6        DO_ICALL                                         $3      
          7        ASSIGN                                                   !1, $3
   79     8        INIT_FCALL                                               'print_r'
          9        INIT_FCALL                                               'sorthighesttolowest'
         10        SEND_VAR                                                 !1
         11        DO_FCALL                                      0  $5      
         12        SEND_VAR                                                 $5
         13        DO_ICALL                                                 
         14      > RETURN                                                   1

Function sorthighesttolowest:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YXP3o
function name:  sortHighestToLowest
number of ops:  10
compiled vars:  !0 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   62     0  E >   RECV                                             !0      
   64     1        INIT_FCALL                                               'array_map'
          2        DECLARE_LAMBDA_FUNCTION                          ~1      [0]
   75     3        SEND_VAL                                                 ~1
          4        SEND_VAR                                                 !0
   64     5        DO_ICALL                                         $2      
   75     6        VERIFY_RETURN_TYPE                                       $2
          7      > RETURN                                                   $2
   76     8*       VERIFY_RETURN_TYPE                                       
          9*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 125) Position 1 = 8, Position 2 = 13
Branch analysis from position: 8
2 jumps found. (Code = 126) Position 1 = 9, Position 2 = 13
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
filename:       /in/YXP3o
function name:  {closure}
number of ops:  18
compiled vars:  !0 = $entries, !1 = $rank, !2 = $entry
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   64     0  E >   RECV                                             !0      
   65     1        INIT_FCALL                                               'uasort'
          2        SEND_REF                                                 !0
          3        DECLARE_LAMBDA_FUNCTION                          ~3      [0]
   67     4        SEND_VAL                                                 ~3
   65     5        DO_ICALL                                                 
   69     6        ASSIGN                                                   !1, 0
   70     7      > FE_RESET_RW                                      $6      !0, ->13
          8    > > FE_FETCH_RW                                              $6, !2, ->13
   71     9    >   PRE_INC                                          ~8      !1
         10        ASSIGN_DIM                                               !2, 'rank'
         11        OP_DATA                                                  ~8
   70    12      > JMP                                                      ->8
         13    >   FE_FREE                                                  $6
   74    14        VERIFY_RETURN_TYPE                                       !0
         15      > RETURN                                                   !0
   75    16*       VERIFY_RETURN_TYPE                                       
         17*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YXP3o
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $entry1, !1 = $entry2
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   65     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   66     2        FETCH_DIM_R                                      ~2      !1, 'avgTotalPoints'
          3        FETCH_DIM_R                                      ~3      !0, 'avgTotalPoints'
          4        SPACESHIP                                        ~4      ~2, ~3
          5        VERIFY_RETURN_TYPE                                       ~4
          6      > RETURN                                                   ~4
   67     7*       VERIFY_RETURN_TYPE                                       
          8*     > RETURN                                                   null

End of Dynamic Function 0

End of Dynamic Function 0

End of function sorthighesttolowest

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
146.33 ms | 1467 KiB | 18 Q