3v4l.org

run code in 200+ PHP versions simultaneously
<?php /** * Re-order the characters from $T using the order of characters in $O. * * @param string $T the string to re-order * @param string $O the string that specifies the desired order * @return string the re-ordered $T */ function reorder($T, $O) { // Convert $T and $O to arrays of characters $t = str_split($T); $o = str_split($O); // Compute the index of each character from $T in $O // Use -1 for characters that do not appear in $O $m = array_map(function($x) use ($o) { $p = array_search($x, $o); return ($p === FALSE) ? -1 : $p; }, $t ); // Sort $m and $t simultaneously; $t will follow the changes applied to $m array_multisort($m, $t); // Join the sorted characters of $T back and return as string return implode('', $t); } $order = 'eloh'; echo('hello => '.reorder('hello', $order)."\n"); echo('help => '.reorder('help', $order)."\n");
based on qqiO5
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/vnoC1
function name:  (null)
number of ops:  16
compiled vars:  !0 = $order
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  31     0  E >   QM_ASSIGN                                                !0
  32     1        INIT_FCALL                                               'reorder'
         2        SEND_VAL                                                 'hello'
         3        SEND_VAR                                                 !0
         4        DO_FCALL                                      0  $2      
         5        CONCAT                                           ~3      'hello+%3D%3E+', $2
         6        CONCAT                                           ~4      ~3, '%0A'
         7        ASSIGN_OBJ                                               ~4
  33     8        INIT_FCALL                                               'reorder'
         9        SEND_VAL                                                 'help'
        10        SEND_VAR                                                 !0
        11        DO_FCALL                                      0  $5      
        12        CONCAT                                           ~6      'help++%3D%3E+', $5
        13        CONCAT                                           ~7      ~6, '%0A'
        14        ASSIGN_OBJ                                               ~7
        15      > RETURN                                                   1

Function reorder:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/vnoC1
function name:  reorder
number of ops:  27
compiled vars:  !0 = $T, !1 = $O, !2 = $t, !3 = $o, !4 = $m
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   9     0  E >   RECV                                             !0      
         1        RECV                                             !1      
  12     2        INIT_FCALL                                               'str_split'
         3        SEND_VAR                                                 !0
         4        DO_ICALL                                         $5      
         5        QM_ASSIGN                                                !2
  13     6        INIT_FCALL                                               'str_split'
         7        SEND_VAR                                                 !1
         8        DO_ICALL                                         $7      
         9        QM_ASSIGN                                                !3
  17    10        INIT_FCALL                                               'array_map'
        11        RAISE_ABSTRACT_ERROR                             ~9      '%00%7Bclosure%7D%2Fin%2FvnoC10x7fcc46e7a25f'
        12        BIND_LEXICAL                                             ~9, !3
  20    13        SEND_VAL                                                 ~9
  21    14        SEND_VAR                                                 !2
        15        DO_ICALL                                         $10     
        16        QM_ASSIGN                                                !4
  25    17        INIT_FCALL                                               'array_multisort'
        18        SEND_REF                                                 !4
        19        SEND_REF                                                 !2
        20        DO_ICALL                                                 
  28    21        INIT_FCALL                                               'implode'
        22        SEND_VAL                                                 ''
        23        SEND_VAR                                                 !2
        24        DO_ICALL                                         $13     
        25      > RETURN                                                   $13
  29    26*     > RETURN                                                   null

End of function reorder

Function %00%7Bclosure%7D%2Fin%2FvnoC10x7fcc46e7a25f:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 11
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/vnoC1
function name:  {closure}
number of ops:  14
compiled vars:  !0 = $x, !1 = $o, !2 = $p
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  17     0  E >   RECV                                             !0      
         1        BIND_STATIC                                              !1
  18     2        INIT_FCALL                                               'array_search'
         3        SEND_VAR                                                 !0
         4        SEND_VAR                                                 !1
         5        DO_ICALL                                         $3      
         6        QM_ASSIGN                                                !2
  19     7        IS_NOT_IDENTICAL                                 ~5      !2, <false>
         8      > JMPZ                                                     ~5, ->11
         9    >   ASSIGN_BW_OR                                  0  ~6      -1
        10      > JMP                                                      ->12
        11    >   ASSIGN_BW_OR                                  0  ~6      !2
        12    > > RETURN                                                   ~6
  20    13*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FvnoC10x7fcc46e7a25f

Generated using Vulcan Logic Dumper, using php 7.4.0