3v4l.org

run code in 500+ 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                              ~14     [0]
          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


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
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 = 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 = 36, Position 2 = 37
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 37
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: 42
2 jumps found. (Code = 43) Position 1 = 57, Position 2 = 61
Branch analysis from position: 57
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 61
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 42
filename:       /in/5DREG
function name:  {closure:/in/5DREG:56}
number of ops:  63
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, ->42
         10    > > FE_FETCH_R                                           ~14     $13, !6, ->42
         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    >   FRAMELESS_ICALL_2                in_array            ~27     !6, !5
         35      > JMPZ                                                         ~27, ->37
   75    36    > > JMP                                                          ->10
   77    37    >   ASSIGN_DIM                                                   !2, !7
         38        OP_DATA                                                      !6
   78    39        ASSIGN_DIM                                                   !5
         40        OP_DATA                                                      !6
   62    41      > JMP                                                          ->10
         42    >   FE_FREE                                                      $13
   82    43        INIT_FCALL                                                   'array_diff_assoc'
         44        SEND_VAR                                                     !0
         45        SEND_VAR                                                     !2
         46        DO_ICALL                                             $30     
         47        ASSIGN                                                       !9, $30
   84    48        INIT_FCALL                                                   'array_unique'
         49        SEND_VAR                                                     !9
         50        DO_ICALL                                             $32     
         51        COUNT                                                ~33     $32
         52        IS_IDENTICAL                                         ~34     ~33, 1
         53        ASSIGN                                                       !3, ~34
   87    54        COUNT                                                ~36     !9
         55        IS_SMALLER                                                   0, ~36
         56      > JMPZ                                                         ~37, ->61
   88    57    >   INIT_DYNAMIC_CALL                                            !1
         58        SEND_VAR_EX                                                  !9
         59        DO_FCALL                                          0  $38     
         60      > RETURN                                                       $38
   90    61    > > RETURN                                                       !2
   91    62*     > RETURN                                                       null

End of Dynamic Function 0

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
171.6 ms | 2253 KiB | 18 Q