3v4l.org

run code in 300+ PHP versions simultaneously
<?php function array_to_xml(array $data) { $document = new DOMDocument(); array_to_xml_aux($data, $document); $document->formatOutput = true; return $document; } function array_to_xml_aux(array $data, DOMNode $parent, $name = null) { foreach ($data as $key => $value) { if ($key[0] == '@') { $parent->setAttribute( substr($key, 1), $value ); continue; } if (is_numeric($key)) { $key = $name; } if (is_array($value)) { $areAllInt = true; foreach(array_keys($value) as $k) { if (!is_int($k)) { $areAllInt = false; break; } } if ($areAllInt) { array_to_xml_aux($value, $parent, $key); } else { $subnode = new DOMElement($key); $parent->appendChild($subnode); array_to_xml_aux($value, $subnode, $key); } } else { $xml_data->appendChild(new DOMElement($key, $value)); } } } $document = array_to_xml( array( 'Products' => array( 'Product' => array('@name' => 'TR-501'), 'Descricao' => array('@text' => '55.180.198 / 46789771'), 'Detalhes' => array( 'Variacao' => array( array( '@modelo' => 'Palio', '@ano' => '2006', '@motor' => '1.0 FIRE 8V (FLEX)', ), array( '@modelo' => 'Palio2', '@ano' => '2007', '@motor' => '1.0 FIRE 9V (FLEX)', ), ) ), ) ) ); echo $document->saveXML();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BquYA
function name:  (null)
number of ops:  8
compiled vars:  !0 = $document
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   INIT_FCALL                                               'array_to_xml'
   50     1        SEND_VAL                                                 <array>
          2        DO_FCALL                                      0  $1      
   47     3        ASSIGN                                                   !0, $1
   69     4        INIT_METHOD_CALL                                         !0, 'saveXML'
          5        DO_FCALL                                      0  $3      
          6        ECHO                                                     $3
          7      > RETURN                                                   1

Function array_to_xml:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BquYA
function name:  array_to_xml
number of ops:  12
compiled vars:  !0 = $data, !1 = $document
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1        NEW                                              $2      'DOMDocument'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $2
    5     4        INIT_FCALL_BY_NAME                                       'array_to_xml_aux'
          5        SEND_VAR_EX                                              !0
          6        SEND_VAR_EX                                              !1
          7        DO_FCALL                                      0          
    6     8        ASSIGN_OBJ                                               !1, 'formatOutput'
          9        OP_DATA                                                  <true>
    7    10      > RETURN                                                   !1
    8    11*     > RETURN                                                   null

End of function array_to_xml

Function array_to_xml_aux:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 66
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 66
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 18
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 23
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 58
Branch analysis from position: 25
2 jumps found. (Code = 77) Position 1 = 30, Position 2 = 37
Branch analysis from position: 30
2 jumps found. (Code = 78) Position 1 = 31, Position 2 = 37
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 36
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
2 jumps found. (Code = 43) Position 1 = 39, Position 2 = 45
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 57
Branch analysis from position: 57
1 jumps found. (Code = 42) Position 1 = 65
Branch analysis from position: 65
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 45
1 jumps found. (Code = 42) Position 1 = 65
Branch analysis from position: 65
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
Branch analysis from position: 37
Branch analysis from position: 37
Branch analysis from position: 58
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 23
Branch analysis from position: 66
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 66
filename:       /in/BquYA
function name:  array_to_xml_aux
number of ops:  68
compiled vars:  !0 = $data, !1 = $parent, !2 = $name, !3 = $value, !4 = $key, !5 = $areAllInt, !6 = $k, !7 = $subnode, !8 = $xml_data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      null
   12     3      > FE_RESET_R                                       $9      !0, ->66
          4    > > FE_FETCH_R                                       ~10     $9, !3, ->66
          5    >   ASSIGN                                                   !4, ~10
   13     6        FETCH_DIM_R                                      ~12     !4, 0
          7        IS_EQUAL                                                 ~12, '%40'
          8      > JMPZ                                                     ~13, ->18
   14     9    >   INIT_METHOD_CALL                                         !1, 'setAttribute'
   15    10        INIT_FCALL                                               'substr'
         11        SEND_VAR                                                 !4
         12        SEND_VAL                                                 1
         13        DO_ICALL                                         $14     
         14        SEND_VAR_NO_REF_EX                                       $14
         15        SEND_VAR_EX                                              !3
         16        DO_FCALL                                      0          
   18    17      > JMP                                                      ->4
   21    18    >   INIT_FCALL                                               'is_numeric'
         19        SEND_VAR                                                 !4
         20        DO_ICALL                                         $16     
         21      > JMPZ                                                     $16, ->23
   22    22    >   ASSIGN                                                   !4, !2
   25    23    >   TYPE_CHECK                                  128          !3
         24      > JMPZ                                                     ~18, ->58
   26    25    >   ASSIGN                                                   !5, <true>
   27    26        INIT_FCALL                                               'array_keys'
         27        SEND_VAR                                                 !3
         28        DO_ICALL                                         $20     
         29      > FE_RESET_R                                       $21     $20, ->37
         30    > > FE_FETCH_R                                               $21, !6, ->37
   28    31    >   TYPE_CHECK                                   16  ~22     !6
         32        BOOL_NOT                                         ~23     ~22
         33      > JMPZ                                                     ~23, ->36
   29    34    >   ASSIGN                                                   !5, <false>
   30    35      > JMP                                                      ->37
   27    36    > > JMP                                                      ->30
         37    >   FE_FREE                                                  $21
   33    38      > JMPZ                                                     !5, ->45
   34    39    >   INIT_FCALL_BY_NAME                                       'array_to_xml_aux'
         40        SEND_VAR_EX                                              !3
         41        SEND_VAR_EX                                              !1
         42        SEND_VAR_EX                                              !4
         43        DO_FCALL                                      0          
         44      > JMP                                                      ->57
   36    45    >   NEW                                              $26     'DOMElement'
         46        SEND_VAR_EX                                              !4
         47        DO_FCALL                                      0          
         48        ASSIGN                                                   !7, $26
   37    49        INIT_METHOD_CALL                                         !1, 'appendChild'
         50        SEND_VAR_EX                                              !7
         51        DO_FCALL                                      0          
   38    52        INIT_FCALL_BY_NAME                                       'array_to_xml_aux'
         53        SEND_VAR_EX                                              !3
         54        SEND_VAR_EX                                              !7
         55        SEND_VAR_EX                                              !4
         56        DO_FCALL                                      0          
         57    > > JMP                                                      ->65
   42    58    >   INIT_METHOD_CALL                                         !8, 'appendChild'
         59        NEW                                              $31     'DOMElement'
         60        SEND_VAR_EX                                              !4
         61        SEND_VAR_EX                                              !3
         62        DO_FCALL                                      0          
         63        SEND_VAR_NO_REF_EX                                       $31
         64        DO_FCALL                                      0          
   12    65    > > JMP                                                      ->4
         66    >   FE_FREE                                                  $9
   45    67      > RETURN                                                   null

End of function array_to_xml_aux

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
170.93 ms | 1407 KiB | 20 Q