3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = array( 'id' => 1, 'name' => 'Jacopo', 'male' => true, 'birthday' => new DateTime('1988-07-26'), 'hobbies' => array( 'Programming', 'TV Shows', 'Skiing' ) ); $rootXMLElement = '<data />'; $xml = new SimpleXMLElementExtended('<?xml version="1.0"?>' . $rootXMLElement); arrayToXML($data, $xml); echo $xml->asXML(); // http://coffeerings.posterous.com/php-simplexml-and-cdata class SimpleXMLElementExtended extends SimpleXMLElement { public function addCDATA($text) { $node = dom_import_simplexml($this); $no = $node->ownerDocument; $node->appendChild($no->createCDATASection($text)); } public function getParentNode() { return current($this->xpath('parent::*')); } } function arrayToXML($data, &$xml) { foreach ($data as $key => $value) { if (is_numeric($key)) { $key = 'item'; } if (is_array($value)) { if (!is_numeric($key)) { $node = $xml->addChild($key); arrayToXML($value, $node); } else { // $currentNodeName = $xml->getName(); // $currentNodeNameSingular = @Inflector::singularize($currentNodeName); // if ( $currentNodeName != 'data' // && $currentNodeName != $currentNodeNameSingular // ) { // $key = $currentNodeNameSingular; // } else { // $key = 'item'; // } $node = $xml->addChild($key); arrayToXML($value, $node); } } else { if (strpos($value, '<') === false) { $xml->addChild($key, $value); } else { $xml->addChild($key)->addCDATA($value); } } } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YG4NO
function name:  (null)
number of ops:  23
compiled vars:  !0 = $data, !1 = $rootXMLElement, !2 = $xml
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   INIT_ARRAY                                       ~3      1, 'id'
    4     1        ADD_ARRAY_ELEMENT                                ~3      'Jacopo', 'name'
    3     2        ADD_ARRAY_ELEMENT                                ~3      <true>, 'male'
    6     3        NEW                                              $4      'DateTime'
          4        SEND_VAL_EX                                              '1988-07-26'
          5        DO_FCALL                                      0          
          6        ADD_ARRAY_ELEMENT                                ~3      $4, 'birthday'
    3     7        ADD_ARRAY_ELEMENT                                ~3      <array>, 'hobbies'
    2     8        ASSIGN                                                   !0, ~3
   14     9        ASSIGN                                                   !1, '%3Cdata+%2F%3E'
   15    10        NEW                                              $8      'SimpleXMLElementExtended'
         11        CONCAT                                           ~9      '%3C%3Fxml+version%3D%221.0%22%3F%3E', !1
         12        SEND_VAL_EX                                              ~9
         13        DO_FCALL                                      0          
         14        ASSIGN                                                   !2, $8
   16    15        INIT_FCALL_BY_NAME                                       'arrayToXML'
         16        SEND_VAR_EX                                              !0
         17        SEND_VAR_EX                                              !2
         18        DO_FCALL                                      0          
   17    19        INIT_METHOD_CALL                                         !2, 'asXML'
         20        DO_FCALL                                      0  $13     
         21        ECHO                                                     $13
   68    22      > RETURN                                                   1

Function arraytoxml:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 53
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 53
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 10
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 35
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 26
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 52
Branch analysis from position: 52
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 52
Branch analysis from position: 52
Branch analysis from position: 35
2 jumps found. (Code = 43) Position 1 = 41, Position 2 = 46
Branch analysis from position: 41
1 jumps found. (Code = 42) Position 1 = 52
Branch analysis from position: 52
Branch analysis from position: 46
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 10
Branch analysis from position: 53
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 53
filename:       /in/YG4NO
function name:  arrayToXML
number of ops:  55
compiled vars:  !0 = $data, !1 = $xml, !2 = $value, !3 = $key, !4 = $node
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   37     2      > FE_RESET_R                                       $5      !0, ->53
          3    > > FE_FETCH_R                                       ~6      $5, !2, ->53
          4    >   ASSIGN                                                   !3, ~6
   38     5        INIT_FCALL                                               'is_numeric'
          6        SEND_VAR                                                 !3
          7        DO_ICALL                                         $8      
          8      > JMPZ                                                     $8, ->10
   39     9    >   ASSIGN                                                   !3, 'item'
   42    10    >   TYPE_CHECK                                  128          !2
         11      > JMPZ                                                     ~10, ->35
   43    12    >   INIT_FCALL                                               'is_numeric'
         13        SEND_VAR                                                 !3
         14        DO_ICALL                                         $11     
         15        BOOL_NOT                                         ~12     $11
         16      > JMPZ                                                     ~12, ->26
   44    17    >   INIT_METHOD_CALL                                         !1, 'addChild'
         18        SEND_VAR_EX                                              !3
         19        DO_FCALL                                      0  $13     
         20        ASSIGN                                                   !4, $13
   45    21        INIT_FCALL_BY_NAME                                       'arrayToXML'
         22        SEND_VAR_EX                                              !2
         23        SEND_VAR_EX                                              !4
         24        DO_FCALL                                      0          
         25      > JMP                                                      ->34
   57    26    >   INIT_METHOD_CALL                                         !1, 'addChild'
         27        SEND_VAR_EX                                              !3
         28        DO_FCALL                                      0  $16     
         29        ASSIGN                                                   !4, $16
   58    30        INIT_FCALL_BY_NAME                                       'arrayToXML'
         31        SEND_VAR_EX                                              !2
         32        SEND_VAR_EX                                              !4
         33        DO_FCALL                                      0          
         34    > > JMP                                                      ->52
   61    35    >   INIT_FCALL                                               'strpos'
         36        SEND_VAR                                                 !2
         37        SEND_VAL                                                 '%3C'
         38        DO_ICALL                                         $19     
         39        TYPE_CHECK                                    4          $19
         40      > JMPZ                                                     ~20, ->46
   62    41    >   INIT_METHOD_CALL                                         !1, 'addChild'
         42        SEND_VAR_EX                                              !3
         43        SEND_VAR_EX                                              !2
         44        DO_FCALL                                      0          
         45      > JMP                                                      ->52
   64    46    >   INIT_METHOD_CALL                                         !1, 'addChild'
         47        SEND_VAR_EX                                              !3
         48        DO_FCALL                                      0  $22     
         49        INIT_METHOD_CALL                                         $22, 'addCDATA'
         50        SEND_VAR_EX                                              !2
         51        DO_FCALL                                      0          
   37    52    > > JMP                                                      ->3
         53    >   FE_FREE                                                  $5
   68    54      > RETURN                                                   null

End of function arraytoxml

Class SimpleXMLElementExtended:
Function addcdata:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YG4NO
function name:  addCDATA
number of ops:  15
compiled vars:  !0 = $text, !1 = $node, !2 = $no
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV                                             !0      
   24     1        INIT_FCALL                                               'dom_import_simplexml'
          2        FETCH_THIS                                       ~3      
          3        SEND_VAL                                                 ~3
          4        DO_ICALL                                         $4      
          5        ASSIGN                                                   !1, $4
   25     6        FETCH_OBJ_R                                      ~6      !1, 'ownerDocument'
          7        ASSIGN                                                   !2, ~6
   26     8        INIT_METHOD_CALL                                         !1, 'appendChild'
          9        INIT_METHOD_CALL                                         !2, 'createCDATASection'
         10        SEND_VAR_EX                                              !0
         11        DO_FCALL                                      0  $8      
         12        SEND_VAR_NO_REF_EX                                       $8
         13        DO_FCALL                                      0          
   27    14      > RETURN                                                   null

End of function addcdata

Function getparentnode:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YG4NO
function name:  getParentNode
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   INIT_FCALL                                               'current'
          1        INIT_METHOD_CALL                                         'xpath'
          2        SEND_VAL_EX                                              'parent%3A%3A%2A'
          3        DO_FCALL                                      0  $0      
          4        SEND_VAR                                                 $0
          5        DO_ICALL                                         $1      
          6      > RETURN                                                   $1
   32     7*     > RETURN                                                   null

End of function getparentnode

End of class SimpleXMLElementExtended.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
158.35 ms | 1404 KiB | 21 Q