3v4l.org

run code in 300+ PHP versions simultaneously
<?php array ( 0 => array ( 'id' => 'users', 'title' => 'Users', 'items' => array ( 0 => array ( 'id' => 'user_1', ), 1 => array ( 'id' => 'user_1', ), ), ), 1 => array ( 'id' => 'players', 'title' => 'Players', 'items' => array ( 0 => array ( 'id' => 'player_1', ), ), ), 2 => array ( 'id' => 'employees', 'title' => 'Employees', 'items' => array ( 0 => array ( 'id' => 'employees_1', ), 1 => array ( 'id' => 'employees_2', ), ), ), 3 => array ( 'id' => 'programmers', 'title' => 'Programmers', 'items' => array ( 0 => array ( 'id' => 'programmer_1', ), 1 => array ( 'id' => 'programmer_2', ), ), ), ); class ArrayCombiner { private $data; public function __construct(array $data) { $this->data = $data; } public function combineAndMerge(array $idsToCombine, $newId, $newTitle) { // Find indices of $idsToCombine in $this->data $indices = $this->findIndicesByIds($idsToCombine); if (count($indices) !== count($idsToCombine)) { return false; // Not all elements were found } // Extract items arrays from $indices $mergedItems = []; foreach ($indices as $index) { $mergedItems = array_merge($mergedItems, $this->data[$index]['items']); } // Create new combined element $combinedElement = [ 'id' => $newId, 'title' => $newTitle, 'items' => $mergedItems, ]; // Remove elements from $this->data based on $indices foreach ($indices as $index) { unset($this->data[$index]); } // Add $combinedElement to $this->data $this->data[] = $combinedElement; // Reindex the array $this->data = array_values($this->data); return true; } public function getData() { return $this->data; } private function findIndicesByIds(array $ids) { $indices = []; foreach ($this->data as $index => $element) { if (in_array($element['id'], $ids)) { $indices[] = $index; } } return $indices; } } // Example usage: $data = [ [ 'id' => 'users', 'title' => 'Users', 'items' => [ ['id' => 'user_1'], ['id' => 'user_2'], ], ], [ 'id' => 'players', 'title' => 'Players', 'items' => [ ['id' => 'player_1'], ], ], [ 'id' => 'employees', 'title' => 'Employees', 'items' => [ ['id' => 'employee_1'], ['id' => 'employee_2'], ], ], [ 'id' => 'programmers', 'title' => 'Programmers', 'items' => [ ['id' => 'programmer_1'], ['id' => 'programmer_2'], ], ], ]; $combiner = new ArrayCombiner($data); // Combine 'users', 'players', 'employees', and 'programmers' into 'combined_group' $combiner->combineAndMerge(['users', 'players', 'employees', 'programmers'], 'combined_group', 'Combined Group'); // Get the modified data $modifiedData = $combiner->getData(); // Display the modified data echo "<pre>"; print_r($modifiedData); echo "</pre>";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/E8PAi
function name:  (null)
number of ops:  19
compiled vars:  !0 = $data, !1 = $combiner, !2 = $modifiedData
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  125     0  E >   ASSIGN                                                   !0, <array>
  159     1        NEW                                              $4      'ArrayCombiner'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !1, $4
  162     5        INIT_METHOD_CALL                                         !1, 'combineAndMerge'
          6        SEND_VAL_EX                                              <array>
          7        SEND_VAL_EX                                              'combined_group'
          8        SEND_VAL_EX                                              'Combined+Group'
          9        DO_FCALL                                      0          
  165    10        INIT_METHOD_CALL                                         !1, 'getData'
         11        DO_FCALL                                      0  $8      
         12        ASSIGN                                                   !2, $8
  168    13        ECHO                                                     '%3Cpre%3E'
  169    14        INIT_FCALL                                               'print_r'
         15        SEND_VAR                                                 !2
         16        DO_ICALL                                                 
  170    17        ECHO                                                     '%3C%2Fpre%3E'
         18      > RETURN                                                   1

Class ArrayCombiner:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/E8PAi
function name:  __construct
number of ops:  4
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   70     0  E >   RECV                                             !0      
   71     1        ASSIGN_OBJ                                               'data'
          2        OP_DATA                                                  !0
   72     3      > RETURN                                                   null

End of function __construct

Function combineandmerge:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 12
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 77) Position 1 = 14, Position 2 = 24
Branch analysis from position: 14
2 jumps found. (Code = 78) Position 1 = 15, Position 2 = 24
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
Branch analysis from position: 24
2 jumps found. (Code = 77) Position 1 = 30, Position 2 = 34
Branch analysis from position: 30
2 jumps found. (Code = 78) Position 1 = 31, Position 2 = 34
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
Branch analysis from position: 24
filename:       /in/E8PAi
function name:  combineAndMerge
number of ops:  46
compiled vars:  !0 = $idsToCombine, !1 = $newId, !2 = $newTitle, !3 = $indices, !4 = $mergedItems, !5 = $index, !6 = $combinedElement
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   74     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   76     3        INIT_METHOD_CALL                                         'findIndicesByIds'
          4        SEND_VAR_EX                                              !0
          5        DO_FCALL                                      0  $7      
          6        ASSIGN                                                   !3, $7
   78     7        COUNT                                            ~9      !3
          8        COUNT                                            ~10     !0
          9        IS_NOT_IDENTICAL                                         ~9, ~10
         10      > JMPZ                                                     ~11, ->12
   79    11    > > RETURN                                                   <false>
   83    12    >   ASSIGN                                                   !4, <array>
   84    13      > FE_RESET_R                                       $13     !3, ->24
         14    > > FE_FETCH_R                                               $13, !5, ->24
   85    15    >   INIT_FCALL                                               'array_merge'
         16        SEND_VAR                                                 !4
         17        FETCH_OBJ_R                                      ~14     'data'
         18        FETCH_DIM_R                                      ~15     ~14, !5
         19        FETCH_DIM_R                                      ~16     ~15, 'items'
         20        SEND_VAL                                                 ~16
         21        DO_ICALL                                         $17     
         22        ASSIGN                                                   !4, $17
   84    23      > JMP                                                      ->14
         24    >   FE_FREE                                                  $13
   90    25        INIT_ARRAY                                       ~19     !1, 'id'
   91    26        ADD_ARRAY_ELEMENT                                ~19     !2, 'title'
   92    27        ADD_ARRAY_ELEMENT                                ~19     !4, 'items'
   89    28        ASSIGN                                                   !6, ~19
   96    29      > FE_RESET_R                                       $21     !3, ->34
         30    > > FE_FETCH_R                                               $21, !5, ->34
   97    31    >   FETCH_OBJ_UNSET                                  $22     'data'
         32        UNSET_DIM                                                $22, !5
   96    33      > JMP                                                      ->30
         34    >   FE_FREE                                                  $21
  101    35        FETCH_OBJ_W                                      $23     'data'
         36        ASSIGN_DIM                                               $23
         37        OP_DATA                                                  !6
  104    38        INIT_FCALL                                               'array_values'
         39        FETCH_OBJ_R                                      ~26     'data'
         40        SEND_VAL                                                 ~26
         41        DO_ICALL                                         $27     
         42        ASSIGN_OBJ                                               'data'
         43        OP_DATA                                                  $27
  106    44      > RETURN                                                   <true>
  107    45*     > RETURN                                                   null

End of function combineandmerge

Function getdata:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/E8PAi
function name:  getData
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  110     0  E >   FETCH_OBJ_R                                      ~0      'data'
          1      > RETURN                                                   ~0
  111     2*     > RETURN                                                   null

End of function getdata

Function findindicesbyids:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 15
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 15
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 14
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 14
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
filename:       /in/E8PAi
function name:  findIndicesByIds
number of ops:  18
compiled vars:  !0 = $ids, !1 = $indices, !2 = $element, !3 = $index
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  113     0  E >   RECV                                             !0      
  114     1        ASSIGN                                                   !1, <array>
  115     2        FETCH_OBJ_R                                      ~5      'data'
          3      > FE_RESET_R                                       $6      ~5, ->15
          4    > > FE_FETCH_R                                       ~7      $6, !2, ->15
          5    >   ASSIGN                                                   !3, ~7
  116     6        INIT_FCALL                                               'in_array'
          7        FETCH_DIM_R                                      ~9      !2, 'id'
          8        SEND_VAL                                                 ~9
          9        SEND_VAR                                                 !0
         10        DO_ICALL                                         $10     
         11      > JMPZ                                                     $10, ->14
  117    12    >   ASSIGN_DIM                                               !1
         13        OP_DATA                                                  !3
  115    14    > > JMP                                                      ->4
         15    >   FE_FREE                                                  $6
  120    16      > RETURN                                                   !1
  121    17*     > RETURN                                                   null

End of function findindicesbyids

End of class ArrayCombiner.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.82 ms | 1022 KiB | 17 Q