3v4l.org

run code in 300+ PHP versions simultaneously
<?php $json = '[ {name: "KFC", distance: 17, likes: 3}, {name: "Macdonalds", distance: 2, likes: 9}, {name: "Pizza Hut", distance: 9, likes: 12}, {name: "Burger King", distance: 14, likes: 17} ]'; $json = preg_replace('/(\w+)(?=:)/', '"$1"', $json); $restaurants = json_decode($json, true); function get_rating($restaurant, $weights) { $rating = 0; foreach ($weights as $factor => $weight) { $rating += $weight * $restaurant[$factor]; } return $rating; } $weights = array('distance' => -30, 'likes' => 10); usort($restaurants, function ($a, $b) use ($weights) { return get_rating($b, $weights) - get_rating($a, $weights); }); print_r($restaurants);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6FLr0
function name:  (null)
number of ops:  23
compiled vars:  !0 = $json, !1 = $restaurants, !2 = $weights
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '%5B%0A+%7Bname%3A+%22KFC%22%2C+distance%3A+17%2C+likes%3A+3%7D%2C%0A+%7Bname%3A+%22Macdonalds%22%2C+distance%3A+2%2C+likes%3A+9%7D%2C%0A+%7Bname%3A+%22Pizza+Hut%22%2C+distance%3A+9%2C+likes%3A+12%7D%2C%0A+%7Bname%3A+%22Burger+King%22%2C+distance%3A+14%2C+likes%3A+17%7D%0A%5D'
    9     1        INIT_FCALL                                               'preg_replace'
          2        SEND_VAL                                                 '%2F%28%5Cw%2B%29%28%3F%3D%3A%29%2F'
          3        SEND_VAL                                                 '%22%241%22'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $4      
          6        ASSIGN                                                   !0, $4
   10     7        INIT_FCALL                                               'json_decode'
          8        SEND_VAR                                                 !0
          9        SEND_VAL                                                 <true>
         10        DO_ICALL                                         $6      
         11        ASSIGN                                                   !1, $6
   20    12        ASSIGN                                                   !2, <array>
   22    13        INIT_FCALL                                               'usort'
         14        SEND_REF                                                 !1
         15        DECLARE_LAMBDA_FUNCTION                          ~9      [0]
         16        BIND_LEXICAL                                             ~9, !2
   24    17        SEND_VAL                                                 ~9
   22    18        DO_ICALL                                                 
   26    19        INIT_FCALL                                               'print_r'
         20        SEND_VAR                                                 !1
         21        DO_ICALL                                                 
         22      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6FLr0
function name:  {closure}
number of ops:  14
compiled vars:  !0 = $a, !1 = $b, !2 = $weights
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        BIND_STATIC                                              !2
   23     3        INIT_FCALL                                               'get_rating'
          4        SEND_VAR                                                 !1
          5        SEND_VAR                                                 !2
          6        DO_FCALL                                      0  $3      
          7        INIT_FCALL                                               'get_rating'
          8        SEND_VAR                                                 !0
          9        SEND_VAR                                                 !2
         10        DO_FCALL                                      0  $4      
         11        SUB                                              ~5      $3, $4
         12      > RETURN                                                   ~5
   24    13*     > RETURN                                                   null

End of Dynamic Function 0

Function get_rating:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 10
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 10
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
filename:       /in/6FLr0
function name:  get_rating
number of ops:  13
compiled vars:  !0 = $restaurant, !1 = $weights, !2 = $rating, !3 = $weight, !4 = $factor
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   13     2        ASSIGN                                                   !2, 0
   14     3      > FE_RESET_R                                       $6      !1, ->10
          4    > > FE_FETCH_R                                       ~7      $6, !3, ->10
          5    >   ASSIGN                                                   !4, ~7
   15     6        FETCH_DIM_R                                      ~9      !0, !4
          7        MUL                                              ~10     !3, ~9
          8        ASSIGN_OP                                     1          !2, ~10
   14     9      > JMP                                                      ->4
         10    >   FE_FREE                                                  $6
   17    11      > RETURN                                                   !2
   18    12*     > RETURN                                                   null

End of function get_rating

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
142.41 ms | 963 KiB | 19 Q