3v4l.org

run code in 300+ PHP versions simultaneously
<?php $values = [ [ 'id' => '2', 'category_id' => 'A', ], [ 'id' => '30', 'category_id' => 'A', ], [ 'id' => '12', 'category_id' => 'B', ], [ 'id' => '8', 'category_id' => 'B', ], [ 'id' => '10', 'category_id' => 'C', ], [ 'id' => '329', 'category_id' => 'D', ], [ 'id' => '89', 'category_id' => 'E', ], [ 'id' => '79', 'category_id' => 'E', ], [ 'id' => '58', 'category_id' => 'A', ], [ 'id' => '219', 'category_id' => 'B', ], [ 'id' => '44', 'category_id' => 'B', ], [ 'id' => '3219', 'category_id' => 'B', ], [ 'id' => '231', 'category_id' => 'B', ], [ 'id' => '43210', 'category_id' => 'F', ] ]; // 一次元配列にする $categoryIds = array_column($values, 'category_id'); // 処理済みの category_id を格納する変数 $sorted = []; // 残っている category_id が一種類のみかどうかを判定する変数 $hasOneSubject = false; $sorting = function ($categoryIds) use (&$sorting, &$sorted, &$hasOneCategory) { // 重複を削除した category_id $uniqueCategory = array_unique($categoryIds); // 使用済みの category_id を格納しておく変数 $used = []; foreach ($categoryIds as $index => $id) { // 処理済みの配列から一番最後を取得 $lastKey = array_key_last($sorted); // 残りの category_id が一種類しかない場合を除き、直近の配列が同じ場合はskip。 if (isset($sorted[$lastKey]) && $sorted[$lastKey] === $id && !$hasOneCategory) { continue; } // category_id を全種類使い切った場合は使用済みをリセットする if (count($uniqueCategory) === count(array_unique($used))) { $used = []; } // 既に同一の category_id を処理済みの場合はskip if (in_array($id, $used)) { continue; } $sorted[$index] = $id; $used[] = $id; } // 残りの配列 $diff = array_diff_assoc($categoryIds, $sorted); // 残りの配列が一種類かどうか $hasOneCategory = count(array_unique($diff)) === 1; // 残っている配列がある場合は再帰処理を実施 if (count($diff) > 0) { return $sorting($diff); } return $sorted; }; $sortedValues = []; // 二次元配列次元配列として入れ直す foreach ($sorting($categoryIds) as $key => $value) { $sortedValues[$key] = $values[$key]; } var_dump($sortedValues);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 18, Position 2 = 24
Branch analysis from position: 18
2 jumps found. (Code = 78) Position 1 = 19, Position 2 = 24
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
filename:       /in/5DREG
function name:  (null)
number of ops:  29
compiled vars:  !0 = $values, !1 = $categoryIds, !2 = $sorted, !3 = $hasOneSubject, !4 = $sorting, !5 = $hasOneCategory, !6 = $sortedValues, !7 = $value, !8 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   50     1        INIT_FCALL                                               'array_column'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 'category_id'
          4        DO_ICALL                                         $10     
          5        ASSIGN                                                   !1, $10
   52     6        ASSIGN                                                   !2, <array>
   54     7        ASSIGN                                                   !3, <false>
   56     8        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F5DREG%3A56%240'
          9        BIND_LEXICAL                                             ~14, !4
         10        BIND_LEXICAL                                             ~14, !2
         11        BIND_LEXICAL                                             ~14, !5
         12        ASSIGN                                                   !4, ~14
   93    13        ASSIGN                                                   !6, <array>
   95    14        INIT_DYNAMIC_CALL                                        !4
         15        SEND_VAR_EX                                              !1
         16        DO_FCALL                                      0  $17     
         17      > FE_RESET_R                                       $18     $17, ->24
         18    > > FE_FETCH_R                                       ~19     $18, !7, ->24
         19    >   ASSIGN                                                   !8, ~19
   96    20        FETCH_DIM_R                                      ~22     !0, !8
         21        ASSIGN_DIM                                               !6, !8
         22        OP_DATA                                                  ~22
   95    23      > JMP                                                      ->18
         24    >   FE_FREE                                                  $18
   98    25        INIT_FCALL                                               'var_dump'
         26        SEND_VAR                                                 !6
         27        DO_ICALL                                                 
         28      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2F5DREG%3A56%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 10, Position 2 = 45
Branch analysis from position: 10
2 jumps found. (Code = 78) Position 1 = 11, Position 2 = 45
Branch analysis from position: 11
2 jumps found. (Code = 46) Position 1 = 18, Position 2 = 21
Branch analysis from position: 18
2 jumps found. (Code = 46) Position 1 = 22, Position 2 = 24
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 26
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 26
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 34
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 39, Position 2 = 40
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 40
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 34
Branch analysis from position: 24
Branch analysis from position: 21
Branch analysis from position: 45
2 jumps found. (Code = 43) Position 1 = 60, Position 2 = 64
Branch analysis from position: 60
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 64
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 45
filename:       /in/5DREG
function name:  {closure}
number of ops:  66
compiled vars:  !0 = $categoryIds, !1 = $sorting, !2 = $sorted, !3 = $hasOneCategory, !4 = $uniqueCategory, !5 = $used, !6 = $id, !7 = $index, !8 = $lastKey, !9 = $diff
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   56     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
          3        BIND_STATIC                                              !3
   58     4        INIT_FCALL                                               'array_unique'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $10     
          7        ASSIGN                                                   !4, $10
   60     8        ASSIGN                                                   !5, <array>
   62     9      > FE_RESET_R                                       $13     !0, ->45
         10    > > FE_FETCH_R                                       ~14     $13, !6, ->45
         11    >   ASSIGN                                                   !7, ~14
   64    12        INIT_FCALL                                               'array_key_last'
         13        SEND_VAR                                                 !2
         14        DO_ICALL                                         $16     
         15        ASSIGN                                                   !8, $16
   66    16        ISSET_ISEMPTY_DIM_OBJ                         0  ~18     !2, !8
         17      > JMPZ_EX                                          ~18     ~18, ->21
         18    >   FETCH_DIM_R                                      ~19     !2, !8
         19        IS_IDENTICAL                                     ~20     !6, ~19
         20        BOOL                                             ~18     ~20
         21    > > JMPZ_EX                                          ~18     ~18, ->24
         22    >   BOOL_NOT                                         ~21     !3
         23        BOOL                                             ~18     ~21
         24    > > JMPZ                                                     ~18, ->26
   67    25    > > JMP                                                      ->10
   70    26    >   COUNT                                            ~22     !4
         27        INIT_FCALL                                               'array_unique'
         28        SEND_VAR                                                 !5
         29        DO_ICALL                                         $23     
         30        COUNT                                            ~24     $23
         31        IS_IDENTICAL                                             ~22, ~24
         32      > JMPZ                                                     ~25, ->34
   71    33    >   ASSIGN                                                   !5, <array>
   74    34    >   INIT_FCALL                                               'in_array'
         35        SEND_VAR                                                 !6
         36        SEND_VAR                                                 !5
         37        DO_ICALL                                         $27     
         38      > JMPZ                                                     $27, ->40
   75    39    > > JMP                                                      ->10
   77    40    >   ASSIGN_DIM                                               !2, !7
         41        OP_DATA                                                  !6
   78    42        ASSIGN_DIM                                               !5
         43        OP_DATA                                                  !6
   62    44      > JMP                                                      ->10
         45    >   FE_FREE                                                  $13
   82    46        INIT_FCALL                                               'array_diff_assoc'
         47        SEND_VAR                                                 !0
         48        SEND_VAR                                                 !2
         49        DO_ICALL                                         $30     
         50        ASSIGN                                                   !9, $30
   84    51        INIT_FCALL                                               'array_unique'
         52        SEND_VAR                                                 !9
         53        DO_ICALL                                         $32     
         54        COUNT                                            ~33     $32
         55        IS_IDENTICAL                                     ~34     ~33, 1
         56        ASSIGN                                                   !3, ~34
   87    57        COUNT                                            ~36     !9
         58        IS_SMALLER                                               0, ~36
         59      > JMPZ                                                     ~37, ->64
   88    60    >   INIT_DYNAMIC_CALL                                        !1
         61        SEND_VAR_EX                                              !9
         62        DO_FCALL                                      0  $38     
         63      > RETURN                                                   $38
   90    64    > > RETURN                                                   !2
   91    65*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F5DREG%3A56%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
149.18 ms | 1409 KiB | 25 Q