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(); $_sorted = array_reverse($_sorted); foreach ($_sorted as $_sortKey => $_sortedData) { if (!count($_newSortedData)) { $_newSortedData = $_sortedData; } else { foreach ($_sortedData as $item) { array_push($_newSortedData,$item); } //$params = $_sortedData; //array_unshift($params,$_newSortedData); //call_user_func_array('array_unshift'), //var_dump($params); //call_user_func_array('array_push',$params); } } $_sorted = $_newSortedData; } else { $_sorted = $_sorted[key($_sorted)]; } 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/DmOX8
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>
   92     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 = 88
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 = 71
Branch analysis from position: 46
2 jumps found. (Code = 77) Position 1 = 52, Position 2 = 68
Branch analysis from position: 52
2 jumps found. (Code = 78) Position 1 = 53, Position 2 = 68
Branch analysis from position: 53
2 jumps found. (Code = 43) Position 1 = 57, Position 2 = 59
Branch analysis from position: 57
1 jumps found. (Code = 42) Position 1 = 67
Branch analysis from position: 67
1 jumps found. (Code = 42) Position 1 = 52
Branch analysis from position: 52
Branch analysis from position: 59
2 jumps found. (Code = 77) Position 1 = 60, Position 2 = 66
Branch analysis from position: 60
2 jumps found. (Code = 78) Position 1 = 61, Position 2 = 66
Branch analysis from position: 61
1 jumps found. (Code = 42) Position 1 = 60
Branch analysis from position: 60
Branch analysis from position: 66
1 jumps found. (Code = 42) Position 1 = 52
Branch analysis from position: 52
Branch analysis from position: 66
Branch analysis from position: 68
1 jumps found. (Code = 42) Position 1 = 76
Branch analysis from position: 76
2 jumps found. (Code = 43) Position 1 = 85, Position 2 = 86
Branch analysis from position: 85
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 86
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 68
Branch analysis from position: 71
2 jumps found. (Code = 43) Position 1 = 85, Position 2 = 86
Branch analysis from position: 85
Branch analysis from position: 86
Branch analysis from position: 42
Branch analysis from position: 88
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
filename:       /in/DmOX8
function name:  sortProducts
number of ops:  90
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 = $item
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  ~14     !0
          5      > JMPZ_EX                                          ~14     ~14, ->8
          6    >   COUNT                                            ~15     !0
          7        BOOL                                             ~14     ~15
          8    > > JMPZ                                                     ~14, ->88
   46     9    > > FE_RESET_R                                       $16     !0, ->42
         10    > > FE_FETCH_R                                               $16, !4, ->42
   47    11    >   INIT_FCALL                                               'preg_match'
         12        SEND_VAL                                                 '%2F%5B0-9%5D%2B%24%2F'
         13        FETCH_DIM_R                                      ~17     !4, 'product_code'
         14        SEND_VAL                                                 ~17
         15        SEND_REF                                                 !3
         16        DO_ICALL                                                 
   48    17        FETCH_DIM_R                                      ~19     !3, 0
         18        ASSIGN                                                   !5, ~19
   50    19        INIT_FCALL                                               'substr'
         20        FETCH_DIM_R                                      ~21     !4, 'product_code'
         21        SEND_VAL                                                 ~21
         22        SEND_VAL                                                 0
         23        INIT_FCALL                                               'strrpos'
         24        FETCH_DIM_R                                      ~22     !4, 'product_code'
         25        SEND_VAL                                                 ~22
         26        SEND_VAR                                                 !5
         27        DO_ICALL                                         $23     
         28        SEND_VAR                                                 $23
         29        DO_ICALL                                         $24     
         30        ASSIGN                                                   !6, $24
   52    31        ISSET_ISEMPTY_CV                                 ~26     !5
         32        BOOL_NOT                                         ~27     ~26
         33      > JMPZ                                                     ~27, ->38
   53    34    >   FETCH_DIM_W                                      $28     !1, !6
         35        ASSIGN_DIM                                               $28, !5
         36        OP_DATA                                                  !4
   54    37      > JMP                                                      ->10
   57    38    >   FETCH_DIM_R                                      ~30     !4, 'product_code'
         39        ASSIGN_DIM                                               !2, ~30
         40        OP_DATA                                                  !4
   46    41      > JMP                                                      ->10
         42    >   FE_FREE                                                  $16
   59    43        COUNT                                            ~32     !1
         44        IS_SMALLER                                               1, ~32
         45      > JMPZ                                                     ~33, ->71
   60    46    >   ASSIGN                                                   !7, <array>
   61    47        INIT_FCALL                                               'array_reverse'
         48        SEND_VAR                                                 !1
         49        DO_ICALL                                         $35     
         50        ASSIGN                                                   !1, $35
   62    51      > FE_RESET_R                                       $37     !1, ->68
         52    > > FE_FETCH_R                                       ~38     $37, !8, ->68
         53    >   ASSIGN                                                   !9, ~38
   63    54        COUNT                                            ~40     !7
         55        BOOL_NOT                                         ~41     ~40
         56      > JMPZ                                                     ~41, ->59
   64    57    >   ASSIGN                                                   !7, !8
         58      > JMP                                                      ->67
   66    59    > > FE_RESET_R                                       $43     !8, ->66
         60    > > FE_FETCH_R                                               $43, !10, ->66
   67    61    >   INIT_FCALL                                               'array_push'
         62        SEND_REF                                                 !7
         63        SEND_VAR                                                 !10
         64        DO_ICALL                                                 
   66    65      > JMP                                                      ->60
         66    >   FE_FREE                                                  $43
   62    67    > > JMP                                                      ->52
         68    >   FE_FREE                                                  $37
   76    69        ASSIGN                                                   !1, !7
         70      > JMP                                                      ->76
   78    71    >   INIT_FCALL                                               'key'
         72        SEND_VAR                                                 !1
         73        DO_ICALL                                         $46     
         74        FETCH_DIM_R                                      ~47     !1, $46
         75        ASSIGN                                                   !1, ~47
   80    76    >   INIT_FCALL                                               'ksort'
         77        SEND_REF                                                 !1
         78        DO_ICALL                                                 
   81    79        INIT_FCALL                                               'ksort'
         80        SEND_REF                                                 !2
         81        DO_ICALL                                                 
   83    82        COUNT                                            ~51     !1
         83        BOOL_NOT                                         ~52     ~51
         84      > JMPZ                                                     ~52, ->86
   84    85    > > RETURN                                                   !2
   87    86    >   ADD                                              ~53     !1, !2
         87      > RETURN                                                   ~53
   89    88    > > RETURN                                                   !0
   90    89*     > RETURN                                                   null

End of function sortproducts

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
150.44 ms | 1407 KiB | 30 Q