3v4l.org

run code in 300+ PHP versions simultaneously
<?php $xml = <<<XML <cats> <cat> <name>Jack</name> <age>2</age> <color>grey</color> <color>white</color> </cat> <cat> <name>Maxwell</name> <age>12</age> <color>orange</color> <color>black</color> </cat> </cats> XML; function xml2array($fname){ $sxi = new SimpleXmlIterator($fname, null, true); return sxiToArray($sxi); } function sxiToArray($sxi){ $a = array(); for( $sxi->rewind(); $sxi->valid(); $sxi->next() ) { if(!array_key_exists($sxi->key(), $a)){ $a[$sxi->key()] = array(); } if($sxi->hasChildren()){ $a[$sxi->key()][] = sxiToArray($sxi->current()); } else{ $a[$sxi->key()][] = strval($sxi->current()); } } return $a; } // Read cats.xml and print the results: $catArray = xml2array($xml); print_r($catArray); ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Snet9
function name:  (null)
number of ops:  9
compiled vars:  !0 = $xml, !1 = $catArray
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '%3Ccats%3E%0A++%3Ccat%3E%0A++++++%3Cname%3EJack%3C%2Fname%3E%0A++++++%3Cage%3E2%3C%2Fage%3E%0A++++++%3Ccolor%3Egrey%3C%2Fcolor%3E%0A++++++%3Ccolor%3Ewhite%3C%2Fcolor%3E%0A++%3C%2Fcat%3E%0A++%3Ccat%3E%0A++++++%3Cname%3EMaxwell%3C%2Fname%3E%0A++++++%3Cage%3E12%3C%2Fage%3E%0A++++++%3Ccolor%3Eorange%3C%2Fcolor%3E%0A++++++%3Ccolor%3Eblack%3C%2Fcolor%3E%0A++%3C%2Fcat%3E%0A%3C%2Fcats%3E'
   42     1        INIT_FCALL                                               'xml2array'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $3      
          4        ASSIGN                                                   !1, $3
   43     5        INIT_FCALL                                               'print_r'
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                                 
   44     8      > RETURN                                                   1

Function xml2array:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Snet9
function name:  xml2array
number of ops:  12
compiled vars:  !0 = $fname, !1 = $sxi
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   RECV                                             !0      
   21     1        NEW                                              $2      'SimpleXmlIterator'
          2        SEND_VAR_EX                                              !0
          3        SEND_VAL_EX                                              null
          4        SEND_VAL_EX                                              <true>
          5        DO_FCALL                                      0          
          6        ASSIGN                                                   !1, $2
   22     7        INIT_FCALL_BY_NAME                                       'sxiToArray'
          8        SEND_VAR_EX                                              !1
          9        DO_FCALL                                      0  $5      
         10      > RETURN                                                   $5
   23    11*     > RETURN                                                   null

End of function xml2array

Function sxitoarray:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
2 jumps found. (Code = 44) Position 1 = 41, Position 2 = 5
Branch analysis from position: 41
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 14
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 28
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
2 jumps found. (Code = 44) Position 1 = 41, Position 2 = 5
Branch analysis from position: 41
Branch analysis from position: 5
Branch analysis from position: 28
2 jumps found. (Code = 44) Position 1 = 41, Position 2 = 5
Branch analysis from position: 41
Branch analysis from position: 5
Branch analysis from position: 14
filename:       /in/Snet9
function name:  sxiToArray
number of ops:  43
compiled vars:  !0 = $sxi, !1 = $a
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
   26     1        ASSIGN                                                   !1, <array>
   27     2        INIT_METHOD_CALL                                         !0, 'rewind'
          3        DO_FCALL                                      0          
          4      > JMP                                                      ->38
   28     5    >   INIT_METHOD_CALL                                         !0, 'key'
          6        DO_FCALL                                      0  $4      
          7        ARRAY_KEY_EXISTS                                 ~5      $4, !1
          8        BOOL_NOT                                         ~6      ~5
          9      > JMPZ                                                     ~6, ->14
   29    10    >   INIT_METHOD_CALL                                         !0, 'key'
         11        DO_FCALL                                      0  $7      
         12        ASSIGN_DIM                                               !1, $7
         13        OP_DATA                                                  <array>
   31    14    >   INIT_METHOD_CALL                                         !0, 'hasChildren'
         15        DO_FCALL                                      0  $9      
         16      > JMPZ                                                     $9, ->28
   32    17    >   INIT_METHOD_CALL                                         !0, 'key'
         18        DO_FCALL                                      0  $10     
         19        INIT_FCALL_BY_NAME                                       'sxiToArray'
         20        INIT_METHOD_CALL                                         !0, 'current'
         21        DO_FCALL                                      0  $13     
         22        SEND_VAR_NO_REF_EX                                       $13
         23        DO_FCALL                                      0  $14     
         24        FETCH_DIM_W                                      $11     !1, $10
         25        ASSIGN_DIM                                               $11
         26        OP_DATA                                                  $14
         27      > JMP                                                      ->36
   35    28    >   INIT_METHOD_CALL                                         !0, 'key'
         29        DO_FCALL                                      0  $15     
         30        INIT_METHOD_CALL                                         !0, 'current'
         31        DO_FCALL                                      0  $18     
         32        CAST                                          6  ~19     $18
         33        FETCH_DIM_W                                      $16     !1, $15
         34        ASSIGN_DIM                                               $16
         35        OP_DATA                                                  ~19
   27    36    >   INIT_METHOD_CALL                                         !0, 'next'
         37        DO_FCALL                                      0          
         38    >   INIT_METHOD_CALL                                         !0, 'valid'
         39        DO_FCALL                                      0  $21     
         40      > JMPNZ                                                    $21, ->5
   38    41    > > RETURN                                                   !1
   39    42*     > RETURN                                                   null

End of function sxitoarray

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
168.25 ms | 1403 KiB | 16 Q