3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = array( array('product_code' => 'battle-for-zendikar-token-1', 'product' => 'battle-for-zendikar-token-1'), array('product_code' => 'battle-for-zendikar-token-2', 'product' => 'battle-for-zendikar-token-2'), array('product_code' => 'battle-for-zendikar-token-3', 'product' => 'battle-for-zendikar-token-3'), array('product_code' => 'battle-for-zendikar-token-4', 'product' => 'battle-for-zendikar-token-4'), array('product_code' => 'battle-for-zendikar-token-5', 'product' => 'battle-for-zendikar-token-5'), array('product_code' => 'battle-for-zendikar-token-6', 'product' => 'battle-for-zendikar-token-6'), array('product_code' => 'battle-for-zendikar-token-7', 'product' => 'battle-for-zendikar-token-7'), array('product_code' => 'battle-for-zendikar-token-8', 'product' => 'battle-for-zendikar-token-8'), array('product_code' => 'battle-for-zendikar-token-9', 'product' => 'battle-for-zendikar-token-9'), array('product_code' => 'battle-for-zendikar-token-10', 'product' => 'battle-for-zendikar-token-10'), array('product_code' => 'battle-for-zendikar-token-11', 'product' => 'battle-for-zendikar-token-11'), array('product_code' => 'battle-for-zendikar-token-12', 'product' => 'battle-for-zendikar-token-12'), array('product_code' => 'battle-for-zendikar-token-13', 'product' => 'battle-for-zendikar-token-13'), array('product_code' => 'battle-for-zendikar-token-14', 'product' => 'battle-for-zendikar-token-14'), array('product_code' => 'battle-for-zendikar-1', 'product' => 'battle-for-zendikar-1'), array('product_code' => 'battle-for-zendikar-2', 'product' => 'battle-for-zendikar-2'), array('product_code' => 'battle-for-zendikar-3', 'product' => 'battle-for-zendikar-3'), array('product_code' => 'battle-for-zendikar-4', 'product' => 'battle-for-zendikar-4'), array('product_code' => 'battle-for-zendikar-5', 'product' => 'battle-for-zendikar-5'), array('product_code' => 'battle-for-zendikar-6', 'product' => 'battle-for-zendikar-6'), array('product_code' => 'battle-for-zendikar-7', 'product' => 'battle-for-zendikar-7'), array('product_code' => 'battle-for-zendikar-8', 'product' => 'battle-for-zendikar-8'), array('product_code' => 'battle-for-zendikar-9', 'product' => 'battle-for-zendikar-9'), array('product_code' => 'battle-for-zendikar-10', 'product' => 'battle-for-zendikar-10'), array('product_code' => 'battle-for-zendikar-11', 'product' => 'battle-for-zendikar-11'), array('product_code' => 'battle-for-zendikar-12', 'product' => 'battle-for-zendikar-12'), array('product_code' => 'battle-for-zendikar-13', 'product' => 'battle-for-zendikar-13'), array('product_code' => 'battle-for-zendikar-14', 'product' => 'battle-for-zendikar-14'), array('product_code' => 'battle-for-zendikar-15', 'product' => 'battle-for-zendikar-15'), array('product_code' => 'battle-for-zendikar-16', 'product' => 'battle-for-zendikar-16'), array('product_code' => 'battle-for-zendikar-17', 'product' => 'battle-for-zendikar-17'), array('product_code' => 'battle-for-zendikar-18', 'product' => 'battle-for-zendikar-18'), array('product_code' => 'battle-for-zendikar-19', 'product' => 'battle-for-zendikar-19'), array('product_code' => 'battle-for-zendikar-20', 'product' => 'battle-for-zendikar-20') ); function sortProducts($products) { $_sorted = array(); $_unsortable = array(); $_numbers = array(); if (is_array($products) && count($products)) { foreach ($products as $product) { preg_match('/[0-9]+$/', $product['product_code'], $_numbers); $_number = $_numbers[0]; $initial_code_part = substr($product['product_code'],0,strrpos($product['product_code'],$_number)); if (!empty($_number)) { $_sorted[$initial_code_part][$_number] = $product; continue; } $_unsortable[$product['product_code']] = $product; } if (count($_sorted) > 1) { $_newSortedData = array(); foreach ($_sorted as $_sortKey => $_sortedData) { if (!count($_newSorteddata)) { $_newSortedData = $_sortedData; } else { $params = $_sortedData; array_shift($params,$_newSortedData); var_dump($params); call_user_func_array('array_push',$params); } } $_sorted = $_newSortedData; } ksort($_sorted); ksort($_unsortable); if (!count($_sorted)) { return $_unsortable; } return $_sorted + $_unsortable; } return $products; } var_dump(sortProducts($arr));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/k01n4
function name:  (null)
number of ops:  8
compiled vars:  !0 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, <array>
   85     1        INIT_FCALL                                               'var_dump'
          2        INIT_FCALL                                               'sortproducts'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $2      
          5        SEND_VAR                                                 $2
          6        DO_ICALL                                                 
          7      > RETURN                                                   1

Function sortproducts:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 6, Position 2 = 8
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 82
Branch analysis from position: 9
2 jumps found. (Code = 77) Position 1 = 10, Position 2 = 42
Branch analysis from position: 10
2 jumps found. (Code = 78) Position 1 = 11, Position 2 = 42
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 38
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 42
2 jumps found. (Code = 43) Position 1 = 46, Position 2 = 70
Branch analysis from position: 46
2 jumps found. (Code = 77) Position 1 = 48, Position 2 = 68
Branch analysis from position: 48
2 jumps found. (Code = 78) Position 1 = 49, Position 2 = 68
Branch analysis from position: 49
2 jumps found. (Code = 43) Position 1 = 53, Position 2 = 55
Branch analysis from position: 53
1 jumps found. (Code = 42) Position 1 = 67
Branch analysis from position: 67
1 jumps found. (Code = 42) Position 1 = 48
Branch analysis from position: 48
Branch analysis from position: 55
1 jumps found. (Code = 42) Position 1 = 48
Branch analysis from position: 48
Branch analysis from position: 68
2 jumps found. (Code = 43) Position 1 = 79, Position 2 = 80
Branch analysis from position: 79
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 80
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 68
Branch analysis from position: 70
Branch analysis from position: 42
Branch analysis from position: 82
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
filename:       /in/k01n4
function name:  sortProducts
number of ops:  84
compiled vars:  !0 = $products, !1 = $_sorted, !2 = $_unsortable, !3 = $_numbers, !4 = $product, !5 = $_number, !6 = $initial_code_part, !7 = $_newSortedData, !8 = $_sortedData, !9 = $_sortKey, !10 = $_newSorteddata, !11 = $params
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   RECV                                             !0      
   41     1        ASSIGN                                                   !1, <array>
   42     2        ASSIGN                                                   !2, <array>
   43     3        ASSIGN                                                   !3, <array>
   45     4        TYPE_CHECK                                  128  ~15     !0
          5      > JMPZ_EX                                          ~15     ~15, ->8
          6    >   COUNT                                            ~16     !0
          7        BOOL                                             ~15     ~16
          8    > > JMPZ                                                     ~15, ->82
   46     9    > > FE_RESET_R                                       $17     !0, ->42
         10    > > FE_FETCH_R                                               $17, !4, ->42
   47    11    >   INIT_FCALL                                               'preg_match'
         12        SEND_VAL                                                 '%2F%5B0-9%5D%2B%24%2F'
         13        FETCH_DIM_R                                      ~18     !4, 'product_code'
         14        SEND_VAL                                                 ~18
         15        SEND_REF                                                 !3
         16        DO_ICALL                                                 
   48    17        FETCH_DIM_R                                      ~20     !3, 0
         18        ASSIGN                                                   !5, ~20
   50    19        INIT_FCALL                                               'substr'
         20        FETCH_DIM_R                                      ~22     !4, 'product_code'
         21        SEND_VAL                                                 ~22
         22        SEND_VAL                                                 0
         23        INIT_FCALL                                               'strrpos'
         24        FETCH_DIM_R                                      ~23     !4, 'product_code'
         25        SEND_VAL                                                 ~23
         26        SEND_VAR                                                 !5
         27        DO_ICALL                                         $24     
         28        SEND_VAR                                                 $24
         29        DO_ICALL                                         $25     
         30        ASSIGN                                                   !6, $25
   52    31        ISSET_ISEMPTY_CV                                 ~27     !5
         32        BOOL_NOT                                         ~28     ~27
         33      > JMPZ                                                     ~28, ->38
   53    34    >   FETCH_DIM_W                                      $29     !1, !6
         35        ASSIGN_DIM                                               $29, !5
         36        OP_DATA                                                  !4
   54    37      > JMP                                                      ->10
   57    38    >   FETCH_DIM_R                                      ~31     !4, 'product_code'
         39        ASSIGN_DIM                                               !2, ~31
         40        OP_DATA                                                  !4
   46    41      > JMP                                                      ->10
         42    >   FE_FREE                                                  $17
   59    43        COUNT                                            ~33     !1
         44        IS_SMALLER                                               1, ~33
         45      > JMPZ                                                     ~34, ->70
   60    46    >   ASSIGN                                                   !7, <array>
   61    47      > FE_RESET_R                                       $36     !1, ->68
         48    > > FE_FETCH_R                                       ~37     $36, !8, ->68
         49    >   ASSIGN                                                   !9, ~37
   62    50        COUNT                                            ~39     !10
         51        BOOL_NOT                                         ~40     ~39
         52      > JMPZ                                                     ~40, ->55
   63    53    >   ASSIGN                                                   !7, !8
         54      > JMP                                                      ->67
   65    55    >   ASSIGN                                                   !11, !8
   66    56        INIT_FCALL                                               'array_shift'
         57        SEND_REF                                                 !11
         58        SEND_VAR                                                 !7
         59        DO_ICALL                                                 
   67    60        INIT_FCALL                                               'var_dump'
         61        SEND_VAR                                                 !11
         62        DO_ICALL                                                 
   68    63        INIT_FCALL                                               'array_push'
         64        SEND_ARRAY                                               !11
         65        CHECK_UNDEF_ARGS                                         
         66        DO_FCALL                                      0          
   61    67    > > JMP                                                      ->48
         68    >   FE_FREE                                                  $36
   71    69        ASSIGN                                                   !1, !7
   73    70    >   INIT_FCALL                                               'ksort'
         71        SEND_REF                                                 !1
         72        DO_ICALL                                                 
   74    73        INIT_FCALL                                               'ksort'
         74        SEND_REF                                                 !2
         75        DO_ICALL                                                 
   76    76        COUNT                                            ~49     !1
         77        BOOL_NOT                                         ~50     ~49
         78      > JMPZ                                                     ~50, ->80
   77    79    > > RETURN                                                   !2
   80    80    >   ADD                                              ~51     !1, !2
         81      > RETURN                                                   ~51
   82    82    > > RETURN                                                   !0
   83    83*     > RETURN                                                   null

End of function sortproducts

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
176.19 ms | 1407 KiB | 28 Q