3v4l.org

run code in 300+ PHP versions simultaneously
<?php function xml_to_array($root) { $result = array(); if ($root->hasAttributes()) { $attrs = $root->attributes; foreach ($attrs as $attr) { $result['@attributes'][$attr->name] = $attr->value; } } if ($root->hasChildNodes()) { $children = $root->childNodes; if ($children->length == 1) { $child = $children->item(0); if ($child->nodeType == XML_TEXT_NODE) { $result['_value'] = $child->nodeValue; return count($result) == 1 ? $result['_value'] : $result; } } $groups = array(); foreach ($children as $child) { if (!isset($result[$child->nodeName])) { $result[$child->nodeName] = xml_to_array($child); } else { if (!isset($groups[$child->nodeName])) { $result[$child->nodeName] = array($result[$child->nodeName]); $groups[$child->nodeName] = 1; } $result[$child->nodeName][] = xml_to_array($child); } } } return $result; } $xml = "<Msg> <Txn>submitsms </Txn> <AccountID>123456789</AccountID> <Password>barfoo123</Password> <Message>test message</Message> <RateCode>1</RateCode> <Mobiles> <MobileNo>07123456701</MobileNo> </Mobiles> </Msg>"; $doc = new DOMDocument(); $doc->loadXML($xml); var_dump(xml_to_array($doc));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Dd2AU
function name:  (null)
number of ops:  14
compiled vars:  !0 = $xml, !1 = $doc
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   ASSIGN                                                   !0, '%3CMsg%3E%0A%09%3CTxn%3Esubmitsms%0A%09%3C%2FTxn%3E%0A%09%3CAccountID%3E123456789%3C%2FAccountID%3E%0A%09%3CPassword%3Ebarfoo123%3C%2FPassword%3E%0A%09%3CMessage%3Etest+message%3C%2FMessage%3E%0A%09%3CRateCode%3E1%3C%2FRateCode%3E%0A%09%3CMobiles%3E%0A%09%09%3CMobileNo%3E07123456701%3C%2FMobileNo%3E%0A%09%3C%2FMobiles%3E%0A%3C%2FMsg%3E'
   55     1        NEW                                              $3      'DOMDocument'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $3
   56     4        INIT_METHOD_CALL                                         !1, 'loadXML'
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0          
   58     7        INIT_FCALL                                               'var_dump'
          8        INIT_FCALL                                               'xml_to_array'
          9        SEND_VAR                                                 !1
         10        DO_FCALL                                      0  $7      
         11        SEND_VAR                                                 $7
         12        DO_ICALL                                                 
         13      > RETURN                                                   1

Function xml_to_array:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 16
Branch analysis from position: 5
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 15
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 15
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 78
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 42
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 42
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 37, Position 2 = 40
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 41
Branch analysis from position: 41
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 40
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 42
2 jumps found. (Code = 77) Position 1 = 44, Position 2 = 77
Branch analysis from position: 44
2 jumps found. (Code = 78) Position 1 = 45, Position 2 = 77
Branch analysis from position: 45
2 jumps found. (Code = 43) Position 1 = 49, Position 2 = 56
Branch analysis from position: 49
1 jumps found. (Code = 42) Position 1 = 76
Branch analysis from position: 76
1 jumps found. (Code = 42) Position 1 = 44
Branch analysis from position: 44
Branch analysis from position: 56
2 jumps found. (Code = 43) Position 1 = 60, Position 2 = 69
Branch analysis from position: 60
1 jumps found. (Code = 42) Position 1 = 44
Branch analysis from position: 44
Branch analysis from position: 69
Branch analysis from position: 77
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 77
Branch analysis from position: 42
Branch analysis from position: 78
Branch analysis from position: 15
Branch analysis from position: 16
filename:       /in/Dd2AU
function name:  xml_to_array
number of ops:  80
compiled vars:  !0 = $root, !1 = $result, !2 = $attrs, !3 = $attr, !4 = $children, !5 = $child, !6 = $groups
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1        ASSIGN                                                   !1, <array>
    6     2        INIT_METHOD_CALL                                         !0, 'hasAttributes'
          3        DO_FCALL                                      0  $8      
          4      > JMPZ                                                     $8, ->16
    7     5    >   FETCH_OBJ_R                                      ~9      !0, 'attributes'
          6        ASSIGN                                                   !2, ~9
    8     7      > FE_RESET_R                                       $11     !2, ->15
          8    > > FE_FETCH_R                                               $11, !3, ->15
    9     9    >   FETCH_OBJ_R                                      ~13     !3, 'name'
         10        FETCH_OBJ_R                                      ~15     !3, 'value'
         11        FETCH_DIM_W                                      $12     !1, '%40attributes'
         12        ASSIGN_DIM                                               $12, ~13
         13        OP_DATA                                                  ~15
    8    14      > JMP                                                      ->8
         15    >   FE_FREE                                                  $11
   13    16    >   INIT_METHOD_CALL                                         !0, 'hasChildNodes'
         17        DO_FCALL                                      0  $16     
         18      > JMPZ                                                     $16, ->78
   14    19    >   FETCH_OBJ_R                                      ~17     !0, 'childNodes'
         20        ASSIGN                                                   !4, ~17
   15    21        FETCH_OBJ_R                                      ~19     !4, 'length'
         22        IS_EQUAL                                                 ~19, 1
         23      > JMPZ                                                     ~20, ->42
   16    24    >   INIT_METHOD_CALL                                         !4, 'item'
         25        SEND_VAL_EX                                              0
         26        DO_FCALL                                      0  $21     
         27        ASSIGN                                                   !5, $21
   17    28        FETCH_OBJ_R                                      ~23     !5, 'nodeType'
         29        IS_EQUAL                                                 ~23, 3
         30      > JMPZ                                                     ~24, ->42
   18    31    >   FETCH_OBJ_R                                      ~26     !5, 'nodeValue'
         32        ASSIGN_DIM                                               !1, '_value'
         33        OP_DATA                                                  ~26
   19    34        COUNT                                            ~27     !1
         35        IS_EQUAL                                                 ~27, 1
         36      > JMPZ                                                     ~28, ->40
   20    37    >   FETCH_DIM_R                                      ~29     !1, '_value'
         38        QM_ASSIGN                                        ~30     ~29
         39      > JMP                                                      ->41
   21    40    >   QM_ASSIGN                                        ~30     !1
         41    > > RETURN                                                   ~30
   24    42    >   ASSIGN                                                   !6, <array>
   25    43      > FE_RESET_R                                       $32     !4, ->77
         44    > > FE_FETCH_R                                               $32, !5, ->77
   26    45    >   FETCH_OBJ_R                                      ~33     !5, 'nodeName'
         46        ISSET_ISEMPTY_DIM_OBJ                         0  ~34     !1, ~33
         47        BOOL_NOT                                         ~35     ~34
         48      > JMPZ                                                     ~35, ->56
   27    49    >   FETCH_OBJ_R                                      ~36     !5, 'nodeName'
         50        INIT_FCALL_BY_NAME                                       'xml_to_array'
         51        SEND_VAR_EX                                              !5
         52        DO_FCALL                                      0  $38     
         53        ASSIGN_DIM                                               !1, ~36
         54        OP_DATA                                                  $38
         55      > JMP                                                      ->76
   29    56    >   FETCH_OBJ_R                                      ~39     !5, 'nodeName'
         57        ISSET_ISEMPTY_DIM_OBJ                         0  ~40     !6, ~39
         58        BOOL_NOT                                         ~41     ~40
         59      > JMPZ                                                     ~41, ->69
   30    60    >   FETCH_OBJ_R                                      ~42     !5, 'nodeName'
         61        FETCH_OBJ_R                                      ~44     !5, 'nodeName'
         62        FETCH_DIM_R                                      ~45     !1, ~44
         63        INIT_ARRAY                                       ~46     ~45
         64        ASSIGN_DIM                                               !1, ~42
         65        OP_DATA                                                  ~46
   31    66        FETCH_OBJ_R                                      ~47     !5, 'nodeName'
         67        ASSIGN_DIM                                               !6, ~47
         68        OP_DATA                                                  1
   33    69    >   FETCH_OBJ_R                                      ~49     !5, 'nodeName'
         70        INIT_FCALL_BY_NAME                                       'xml_to_array'
         71        SEND_VAR_EX                                              !5
         72        DO_FCALL                                      0  $52     
         73        FETCH_DIM_W                                      $50     !1, ~49
         74        ASSIGN_DIM                                               $50
         75        OP_DATA                                                  $52
   25    76    > > JMP                                                      ->44
         77    >   FE_FREE                                                  $32
   38    78    > > RETURN                                                   !1
   39    79*     > RETURN                                                   null

End of function xml_to_array

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
146.92 ms | 1407 KiB | 16 Q