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

End of function sortproducts

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
182.89 ms | 1407 KiB | 28 Q