3v4l.org

run code in 300+ PHP versions simultaneously
<?php function arrayToXml($data, \SimpleXmlElement $element, $parent = null) { foreach ($data as $key => $value) { if (is_array($value) || $value instanceof \Traversable) { if (!is_numeric($key)) { if (count($value) > 0 && isset($value[0])) { arrayToXml($value, $element, $key); } else { $subnode = $element->addChild($key); $arrayToXml($value, $subnode, $key); } } else { $subnode = $element->addChild($parent); arrayToXml($value, $subnode, $parent); } } else { if (!is_numeric($key)) { if (substr($key, 0, 1) === '@') { $element->addAttribute(substr($key, 1), $value); } elseif ($key === 'value' and count($data) === 1) { $element->{0} = $value; } elseif (is_bool($value)) { $element->addChild($key, intval($value)); } else { $element->addChild($key, htmlspecialchars($value, ENT_QUOTES)); } } else { $element->addChild($parent, htmlspecialchars($value, ENT_QUOTES)); } } } } $data = array('x' => array('value' => 'test')); $doc = new \SimpleXMLElement('<resource href="/"></resource>'); arrayToXml($data, $doc); echo $doc->x; //$xml = new \SimpleXMLElement('<resource href="/"><x><0>test</0></x></resource>'); //echo $xml->x;
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/C2qHt
function name:  (null)
number of ops:  12
compiled vars:  !0 = $data, !1 = $doc
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   ASSIGN                                                   !0, <array>
   36     1        NEW                                              $3      'SimpleXMLElement'
          2        SEND_VAL_EX                                              '%3Cresource+href%3D%22%2F%22%3E%3C%2Fresource%3E'
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !1, $3
   38     5        INIT_FCALL                                               'arraytoxml'
          6        SEND_VAR                                                 !0
          7        SEND_VAR                                                 !1
          8        DO_FCALL                                      0          
   40     9        FETCH_OBJ_R                                      ~7      !1, 'x'
         10        ECHO                                                     ~7
   42    11      > RETURN                                                   1

Function arraytoxml:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 104
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 104
Branch analysis from position: 5
2 jumps found. (Code = 47) Position 1 = 8, Position 2 = 10
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 48
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 38
Branch analysis from position: 16
2 jumps found. (Code = 46) Position 1 = 19, Position 2 = 21
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 28
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 103
Branch analysis from position: 103
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
Branch analysis from position: 21
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 103
Branch analysis from position: 103
Branch analysis from position: 48
2 jumps found. (Code = 43) Position 1 = 53, Position 2 = 95
Branch analysis from position: 53
2 jumps found. (Code = 43) Position 1 = 60, Position 2 = 69
Branch analysis from position: 60
1 jumps found. (Code = 42) Position 1 = 94
Branch analysis from position: 94
1 jumps found. (Code = 42) Position 1 = 103
Branch analysis from position: 103
Branch analysis from position: 69
2 jumps found. (Code = 46) Position 1 = 71, Position 2 = 74
Branch analysis from position: 71
2 jumps found. (Code = 43) Position 1 = 75, Position 2 = 78
Branch analysis from position: 75
1 jumps found. (Code = 42) Position 1 = 94
Branch analysis from position: 94
Branch analysis from position: 78
2 jumps found. (Code = 43) Position 1 = 80, Position 2 = 86
Branch analysis from position: 80
1 jumps found. (Code = 42) Position 1 = 94
Branch analysis from position: 94
Branch analysis from position: 86
1 jumps found. (Code = 42) Position 1 = 103
Branch analysis from position: 103
Branch analysis from position: 74
Branch analysis from position: 95
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 10
Branch analysis from position: 104
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 104
filename:       /in/C2qHt
function name:  arrayToXml
number of ops:  106
compiled vars:  !0 = $data, !1 = $element, !2 = $parent, !3 = $value, !4 = $key, !5 = $subnode, !6 = $arrayToXml
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      null
    4     3      > FE_RESET_R                                       $7      !0, ->104
          4    > > FE_FETCH_R                                       ~8      $7, !3, ->104
          5    >   ASSIGN                                                   !4, ~8
    5     6        TYPE_CHECK                                  128  ~10     !3
          7      > JMPNZ_EX                                         ~10     ~10, ->10
          8    >   INSTANCEOF                                       ~11     !3, 'Traversable'
          9        BOOL                                             ~10     ~11
         10    > > JMPZ                                                     ~10, ->48
    6    11    >   INIT_FCALL                                               'is_numeric'
         12        SEND_VAR                                                 !4
         13        DO_ICALL                                         $12     
         14        BOOL_NOT                                         ~13     $12
         15      > JMPZ                                                     ~13, ->38
    7    16    >   COUNT                                            ~14     !3
         17        IS_SMALLER                                       ~15     0, ~14
         18      > JMPZ_EX                                          ~15     ~15, ->21
         19    >   ISSET_ISEMPTY_DIM_OBJ                         0  ~16     !3, 0
         20        BOOL                                             ~15     ~16
         21    > > JMPZ                                                     ~15, ->28
    8    22    >   INIT_FCALL_BY_NAME                                       'arrayToXml'
         23        SEND_VAR_EX                                              !3
         24        SEND_VAR_EX                                              !1
         25        SEND_VAR_EX                                              !4
         26        DO_FCALL                                      0          
         27      > JMP                                                      ->37
   10    28    >   INIT_METHOD_CALL                                         !1, 'addChild'
         29        SEND_VAR_EX                                              !4
         30        DO_FCALL                                      0  $18     
         31        ASSIGN                                                   !5, $18
   11    32        INIT_DYNAMIC_CALL                                        !6
         33        SEND_VAR_EX                                              !3
         34        SEND_VAR_EX                                              !5
         35        SEND_VAR_EX                                              !4
         36        DO_FCALL                                      0          
         37    > > JMP                                                      ->47
   14    38    >   INIT_METHOD_CALL                                         !1, 'addChild'
         39        SEND_VAR_EX                                              !2
         40        DO_FCALL                                      0  $21     
         41        ASSIGN                                                   !5, $21
   15    42        INIT_FCALL_BY_NAME                                       'arrayToXml'
         43        SEND_VAR_EX                                              !3
         44        SEND_VAR_EX                                              !5
         45        SEND_VAR_EX                                              !2
         46        DO_FCALL                                      0          
         47    > > JMP                                                      ->103
   18    48    >   INIT_FCALL                                               'is_numeric'
         49        SEND_VAR                                                 !4
         50        DO_ICALL                                         $24     
         51        BOOL_NOT                                         ~25     $24
         52      > JMPZ                                                     ~25, ->95
   19    53    >   INIT_FCALL                                               'substr'
         54        SEND_VAR                                                 !4
         55        SEND_VAL                                                 0
         56        SEND_VAL                                                 1
         57        DO_ICALL                                         $26     
         58        IS_IDENTICAL                                             $26, '%40'
         59      > JMPZ                                                     ~27, ->69
   20    60    >   INIT_METHOD_CALL                                         !1, 'addAttribute'
         61        INIT_FCALL                                               'substr'
         62        SEND_VAR                                                 !4
         63        SEND_VAL                                                 1
         64        DO_ICALL                                         $28     
         65        SEND_VAR_NO_REF_EX                                       $28
         66        SEND_VAR_EX                                              !3
         67        DO_FCALL                                      0          
         68      > JMP                                                      ->94
   21    69    >   IS_IDENTICAL                                     ~30     !4, 'value'
         70      > JMPZ_EX                                          ~30     ~30, ->74
         71    >   COUNT                                            ~31     !0
         72        IS_IDENTICAL                                     ~32     ~31, 1
         73        BOOL                                             ~30     ~32
         74    > > JMPZ                                                     ~30, ->78
   22    75    >   ASSIGN_OBJ                                               !1, '0'
         76        OP_DATA                                                  !3
         77      > JMP                                                      ->94
   23    78    >   TYPE_CHECK                                   12          !3
         79      > JMPZ                                                     ~34, ->86
   24    80    >   INIT_METHOD_CALL                                         !1, 'addChild'
         81        SEND_VAR_EX                                              !4
         82        CAST                                          4  ~35     !3
         83        SEND_VAL_EX                                              ~35
         84        DO_FCALL                                      0          
         85      > JMP                                                      ->94
   26    86    >   INIT_METHOD_CALL                                         !1, 'addChild'
         87        SEND_VAR_EX                                              !4
         88        INIT_FCALL                                               'htmlspecialchars'
         89        SEND_VAR                                                 !3
         90        SEND_VAL                                                 3
         91        DO_ICALL                                         $37     
         92        SEND_VAR_NO_REF_EX                                       $37
         93        DO_FCALL                                      0          
         94    > > JMP                                                      ->103
   29    95    >   INIT_METHOD_CALL                                         !1, 'addChild'
         96        SEND_VAR_EX                                              !2
         97        INIT_FCALL                                               'htmlspecialchars'
         98        SEND_VAR                                                 !3
         99        SEND_VAL                                                 3
        100        DO_ICALL                                         $39     
        101        SEND_VAR_NO_REF_EX                                       $39
        102        DO_FCALL                                      0          
    4   103    > > JMP                                                      ->4
        104    >   FE_FREE                                                  $7
   33   105      > RETURN                                                   null

End of function arraytoxml

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
160.04 ms | 1411 KiB | 20 Q