3v4l.org

run code in 500+ PHP versions simultaneously
<?php $test = [ 'a', 'b', 'a', 'b', 'a', 'a', 'a' ]; $result = []; // The first two need to be setup manually. There should probably be a guard around this // with a length check for $test. $result[] = array_shift($test); $result[] = array_shift($test); // Inside the loop we'll keep removing items while(count($test)){ // Setup common variables $current = array_shift($test); $last = end($result); $last_char = $last[-1]; if(strlen($last) >= 2){ // If we have two (or more) characters at the end of the array, just append $result[] = $current; }elseif($current === $last_char){ // Also, if we have the same character as last time, just append $result[] = $current; }else{ // Otherwise we have a different character, so append the current to the previus $result[ count($result) - 1 ] = $last_char . $current; } } var_dump($result);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 39
Branch analysis from position: 39
2 jumps found. (Code = 44) Position 1 = 41, Position 2 = 13
Branch analysis from position: 41
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 29
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 39
Branch analysis from position: 39
Branch analysis from position: 29
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 34
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 39
Branch analysis from position: 39
Branch analysis from position: 34
2 jumps found. (Code = 44) Position 1 = 41, Position 2 = 13
Branch analysis from position: 41
Branch analysis from position: 13
filename:       /in/0CS8m
function name:  (null)
number of ops:  45
compiled vars:  !0 = $test, !1 = $result, !2 = $current, !3 = $last, !4 = $last_char
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                       !0, <array>
   12     1        ASSIGN                                                       !1, <array>
   16     2        INIT_FCALL                                                   'array_shift'
          3        SEND_REF                                                     !0
          4        DO_ICALL                                             $8      
          5        ASSIGN_DIM                                                   !1
          6        OP_DATA                                                      $8
   17     7        INIT_FCALL                                                   'array_shift'
          8        SEND_REF                                                     !0
          9        DO_ICALL                                             $10     
         10        ASSIGN_DIM                                                   !1
         11        OP_DATA                                                      $10
   20    12      > JMP                                                          ->39
   23    13    >   INIT_FCALL                                                   'array_shift'
         14        SEND_REF                                                     !0
         15        DO_ICALL                                             $11     
         16        ASSIGN                                                       !2, $11
   24    17        INIT_FCALL                                                   'end'
         18        SEND_REF                                                     !1
         19        DO_ICALL                                             $13     
         20        ASSIGN                                                       !3, $13
   25    21        FETCH_DIM_R                                          ~15     !3, -1
         22        ASSIGN                                                       !4, ~15
   27    23        STRLEN                                               ~17     !3
         24        IS_SMALLER_OR_EQUAL                                          2, ~17
         25      > JMPZ                                                         ~18, ->29
   29    26    >   ASSIGN_DIM                                                   !1
         27        OP_DATA                                                      !2
   27    28      > JMP                                                          ->39
   30    29    >   IS_IDENTICAL                                                 !2, !4
         30      > JMPZ                                                         ~20, ->34
   32    31    >   ASSIGN_DIM                                                   !1
         32        OP_DATA                                                      !2
   30    33      > JMP                                                          ->39
   35    34    >   COUNT                                                ~22     !1
         35        SUB                                                  ~23     ~22, 1
         36        CONCAT                                               ~25     !4, !2
         37        ASSIGN_DIM                                                   !1, ~23
         38        OP_DATA                                                      ~25
   20    39    >   COUNT                                                ~26     !0
         40      > JMPNZ                                                        ~26, ->13
   39    41    >   INIT_FCALL                                                   'var_dump'
         42        SEND_VAR                                                     !1
         43        DO_ICALL                                                     
         44      > RETURN                                                       1

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
187.41 ms | 2052 KiB | 16 Q