3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ [ "id" => "1", "macros" => [ "calorie" => 487.98, "fat" => 48.79, "carb" => 7.35, "protein" => 8.06 ], ], [ "id" => "3", "macros" => [ "calorie" => 472.07, "fat" => 38.4, "carb" => 9.46, "protein" => 18.25 ], ], [ "id" => "9", "macros" => [ "calorie" => 445.37, "fat" => 41.15, "carb" => 9.25, "protein" => 10.13 ], ], [ "id" => "18", "macros" => [ "calorie" => 457.73, "fat" => 41.92, "carb" => 5.61, "protein" => 10.94 ], ], [ "id" => "19", "macros" => [ "calorie" => 455.22, "fat" => 42.08, "carb" => 5.25, "protein" => 15.81 ] ], [ "id" => "200", "macros" => [ "calorie" => 443.21, "fat" => 35.95, "carb" => 3.86, "protein" => 24.3 ], ] ]; $optimal = [ "calorie" => 447.53, "fat" => 33.22, "carb" => 3.75, "protein" => 25.93 ]; $minMax = [ "calorie" => [ "minimum" => 406.84, "maximum" => 488.21 ], "fat" => [ "minimum" => 28.7, "maximum" => 37.74 ], "carb" => [ "minimum" => 0, "maximum" => 7.5 ], "protein" => [ "minimum" => 22.22, "maximum" => 29.63 ] ]; function sorter(array $array, $optimal, $minMax){ usort($array, function ($a, $b) use ($optimal, $minMax) { // check closeness of protein to optimal $apo = abs($a['macros']['protein'] - $optimal['protein']); $bpo = abs($b['macros']['protein'] - $optimal['protein']); // if not equally close, return the closest if ($apo != $bpo) return $apo <=> $bpo; // same protein difference to optimal, return the one with least carbs return $a['macros']['carb'] <=> $b['macros']['carb']; }); return $array; } print_r(sorter($array, $optimal, $minMax));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1MQ8Z
function name:  (null)
number of ops:  12
compiled vars:  !0 = $array, !1 = $optimal, !2 = $minMax
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   60     1        ASSIGN                                                   !1, <array>
   67     2        ASSIGN                                                   !2, <array>
   99     3        INIT_FCALL                                               'print_r'
          4        INIT_FCALL                                               'sorter'
          5        SEND_VAR                                                 !0
          6        SEND_VAR                                                 !1
          7        SEND_VAR                                                 !2
          8        DO_FCALL                                      0  $6      
          9        SEND_VAR                                                 $6
         10        DO_ICALL                                                 
         11      > RETURN                                                   1

Function sorter:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1MQ8Z
function name:  sorter
number of ops:  12
compiled vars:  !0 = $array, !1 = $optimal, !2 = $minMax
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   86     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   87     3        INIT_FCALL                                               'usort'
          4        SEND_REF                                                 !0
          5        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F1MQ8Z%3A87%240'
          6        BIND_LEXICAL                                             ~3, !1
          7        BIND_LEXICAL                                             ~3, !2
   95     8        SEND_VAL                                                 ~3
          9        DO_ICALL                                                 
   96    10      > RETURN                                                   !0
   97    11*     > RETURN                                                   null

End of function sorter

Function %00%7Bclosure%7D%2Fin%2F1MQ8Z%3A87%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 24
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1MQ8Z
function name:  {closure}
number of ops:  31
compiled vars:  !0 = $a, !1 = $b, !2 = $optimal, !3 = $minMax, !4 = $apo, !5 = $bpo
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   87     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        BIND_STATIC                                              !2
          3        BIND_STATIC                                              !3
   89     4        INIT_FCALL                                               'abs'
          5        FETCH_DIM_R                                      ~6      !0, 'macros'
          6        FETCH_DIM_R                                      ~7      ~6, 'protein'
          7        FETCH_DIM_R                                      ~8      !2, 'protein'
          8        SUB                                              ~9      ~7, ~8
          9        SEND_VAL                                                 ~9
         10        DO_ICALL                                         $10     
         11        ASSIGN                                                   !4, $10
   90    12        INIT_FCALL                                               'abs'
         13        FETCH_DIM_R                                      ~12     !1, 'macros'
         14        FETCH_DIM_R                                      ~13     ~12, 'protein'
         15        FETCH_DIM_R                                      ~14     !2, 'protein'
         16        SUB                                              ~15     ~13, ~14
         17        SEND_VAL                                                 ~15
         18        DO_ICALL                                         $16     
         19        ASSIGN                                                   !5, $16
   92    20        IS_NOT_EQUAL                                             !4, !5
         21      > JMPZ                                                     ~18, ->24
         22    >   SPACESHIP                                        ~19     !4, !5
         23      > RETURN                                                   ~19
   94    24    >   FETCH_DIM_R                                      ~20     !0, 'macros'
         25        FETCH_DIM_R                                      ~21     ~20, 'carb'
         26        FETCH_DIM_R                                      ~22     !1, 'macros'
         27        FETCH_DIM_R                                      ~23     ~22, 'carb'
         28        SPACESHIP                                        ~24     ~21, ~23
         29      > RETURN                                                   ~24
   95    30*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F1MQ8Z%3A87%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
305.35 ms | 1406 KiB | 22 Q