3v4l.org

run code in 300+ PHP versions simultaneously
<?php function toXml(SimpleXMLElement $xml, array $data, $mainKey = null) { foreach ($data as $key => $value) { // if the key is an integer, it needs text with it to actually work. if (is_numeric($key)) { $key = $mainKey ? : "key_$key"; } if (is_array($value)) { $childXml = $xml->addChild($key); toXml($childXml, $value, substr($key, 0, -1)); } else { $xml->addChild($key, $value); } } return $xml; } // Pretty print Xml function formatXml($simpleXMLElement) { $xmlDocument = new DOMDocument('1.0'); $xmlDocument->preserveWhiteSpace = false; $xmlDocument->formatOutput = true; $xmlDocument->loadXML($simpleXMLElement->asXML()); return $xmlDocument->saveXML(); } $arr_albums = array(); $rows1 = array(array('Id' => 4)); $rows3 = array(array('Id' => 2, 'Name' => 'viktor_rood.png'), array('Id' => 5, 'Name' => 'viktor_geel.png'), array('Id' => 7, 'Name' => 'viktor_paars.png'), array('Id' => 8, 'Name' => 'viktor_aqua.png') ); foreach ($rows1 as $row1) { $arr_images = array(); foreach ($rows3 as $row3) { $image_array = array( 'Id' => $row3["Id"], 'Name' => $row3["Name"] ); $arr_images[] = $image_array; } $album_array = array( 'Id' => $row1["Id"], 'Images' => $arr_images ); $arr_albums[] = $album_array; } $xml = toXml(new SimpleXMLElement('<Albums/>'), $arr_albums, 'Album'); print formatXml($xml);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 24
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 24
Branch analysis from position: 5
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 16
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 16
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 16
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
filename:       /in/B6Qfl
function name:  (null)
number of ops:  39
compiled vars:  !0 = $arr_albums, !1 = $rows1, !2 = $rows3, !3 = $row1, !4 = $arr_images, !5 = $row3, !6 = $image_array, !7 = $album_array, !8 = $xml
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   ASSIGN                                                   !0, <array>
   32     1        ASSIGN                                                   !1, <array>
   33     2        ASSIGN                                                   !2, <array>
   39     3      > FE_RESET_R                                       $12     !1, ->24
          4    > > FE_FETCH_R                                               $12, !3, ->24
   40     5    >   ASSIGN                                                   !4, <array>
   41     6      > FE_RESET_R                                       $14     !2, ->16
          7    > > FE_FETCH_R                                               $14, !5, ->16
   43     8    >   FETCH_DIM_R                                      ~15     !5, 'Id'
          9        INIT_ARRAY                                       ~16     ~15, 'Id'
   44    10        FETCH_DIM_R                                      ~17     !5, 'Name'
         11        ADD_ARRAY_ELEMENT                                ~16     ~17, 'Name'
   42    12        ASSIGN                                                   !6, ~16
   46    13        ASSIGN_DIM                                               !4
         14        OP_DATA                                                  !6
   41    15      > JMP                                                      ->7
         16    >   FE_FREE                                                  $14
   49    17        FETCH_DIM_R                                      ~20     !3, 'Id'
         18        INIT_ARRAY                                       ~21     ~20, 'Id'
   50    19        ADD_ARRAY_ELEMENT                                ~21     !4, 'Images'
   48    20        ASSIGN                                                   !7, ~21
   52    21        ASSIGN_DIM                                               !0
         22        OP_DATA                                                  !7
   39    23      > JMP                                                      ->4
         24    >   FE_FREE                                                  $12
   55    25        INIT_FCALL                                               'toxml'
         26        NEW                                              $24     'SimpleXMLElement'
         27        SEND_VAL_EX                                              '%3CAlbums%2F%3E'
         28        DO_FCALL                                      0          
         29        SEND_VAR                                                 $24
         30        SEND_VAR                                                 !0
         31        SEND_VAL                                                 'Album'
         32        DO_FCALL                                      0  $26     
         33        ASSIGN                                                   !8, $26
   57    34        INIT_FCALL                                               'formatxml'
         35        SEND_VAR                                                 !8
         36        DO_FCALL                                      0  $28     
         37        ECHO                                                     $28
         38      > RETURN                                                   1

Function toxml:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 37
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 37
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 15
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 32
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 15
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
filename:       /in/B6Qfl
function name:  toXml
number of ops:  40
compiled vars:  !0 = $xml, !1 = $data, !2 = $mainKey, !3 = $value, !4 = $key, !5 = $childXml
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      null
    5     3      > FE_RESET_R                                       $6      !1, ->37
          4    > > FE_FETCH_R                                       ~7      $6, !3, ->37
          5    >   ASSIGN                                                   !4, ~7
    7     6        INIT_FCALL                                               'is_numeric'
          7        SEND_VAR                                                 !4
          8        DO_ICALL                                         $9      
          9      > JMPZ                                                     $9, ->15
    8    10    >   JMP_SET                                          ~10     !2, ->14
         11        NOP                                                      
         12        FAST_CONCAT                                      ~11     'key_', !4
         13        QM_ASSIGN                                        ~10     ~11
         14        ASSIGN                                                   !4, ~10
   10    15    >   TYPE_CHECK                                  128          !3
         16      > JMPZ                                                     ~13, ->32
   11    17    >   INIT_METHOD_CALL                                         !0, 'addChild'
         18        SEND_VAR_EX                                              !4
         19        DO_FCALL                                      0  $14     
         20        ASSIGN                                                   !5, $14
   12    21        INIT_FCALL_BY_NAME                                       'toXml'
         22        SEND_VAR_EX                                              !5
         23        SEND_VAR_EX                                              !3
         24        INIT_FCALL                                               'substr'
         25        SEND_VAR                                                 !4
         26        SEND_VAL                                                 0
         27        SEND_VAL                                                 -1
         28        DO_ICALL                                         $16     
         29        SEND_VAR_NO_REF_EX                                       $16
         30        DO_FCALL                                      0          
   10    31      > JMP                                                      ->36
   14    32    >   INIT_METHOD_CALL                                         !0, 'addChild'
         33        SEND_VAR_EX                                              !4
         34        SEND_VAR_EX                                              !3
         35        DO_FCALL                                      0          
    5    36    > > JMP                                                      ->4
         37    >   FE_FREE                                                  $6
   17    38      > RETURN                                                   !0
   18    39*     > RETURN                                                   null

End of function toxml

Function formatxml:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/B6Qfl
function name:  formatXml
number of ops:  18
compiled vars:  !0 = $simpleXMLElement, !1 = $xmlDocument
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
   23     1        NEW                                              $2      'DOMDocument'
          2        SEND_VAL_EX                                              '1.0'
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !1, $2
   24     5        ASSIGN_OBJ                                               !1, 'preserveWhiteSpace'
          6        OP_DATA                                                  <false>
   25     7        ASSIGN_OBJ                                               !1, 'formatOutput'
          8        OP_DATA                                                  <true>
   26     9        INIT_METHOD_CALL                                         !1, 'loadXML'
         10        INIT_METHOD_CALL                                         !0, 'asXML'
         11        DO_FCALL                                      0  $7      
         12        SEND_VAR_NO_REF_EX                                       $7
         13        DO_FCALL                                      0          
   28    14        INIT_METHOD_CALL                                         !1, 'saveXML'
         15        DO_FCALL                                      0  $9      
         16      > RETURN                                                   $9
   29    17*     > RETURN                                                   null

End of function formatxml

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
153.58 ms | 1017 KiB | 17 Q