3v4l.org

run code in 300+ PHP versions simultaneously
<?php // From: https://www.kerstner.at/en/2011/12/php-array-to-xml-conversion/ function arrayToXml($array, $rootElement = null, $xml = null) { $_xml = $xml; if ($_xml === null) { $_xml = new SimpleXMLElement($rootElement !== null ? $rootElement : '<root/>'); } foreach ($array as $k => $v) { if (is_array($v)) { //nested array arrayToXml($v, $k, $_xml->addChild($k)); } else { $_xml->addChild($k, $v); } } return $_xml; } $requestResult = <<<EOT { "name": "Benjamin Clementine", "mbid_id": "578e5d1c-be21-4158-b147-30aad67f207d", "albums": { "68e65535-f133-408d-93dd-205e9555eaa5": { "albumcover": [ { "id": "172070", "url": "http://assets.fanart.tv/fanart/music/578e5d1c-be21-4158-b147-30aad67f207d/albumcover/at-least-for-now-55b74dfa59411.jpg", "likes": "0" } ] } } } EOT; $decodedResult = json_decode($requestResult, true); $xmlElement = arrayToXml($decodedResult); var_dump($xmlElement->xpath('/root/albums/*/albumcover/*/url[1]')[0][0]);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rn6Km
function name:  (null)
number of ops:  19
compiled vars:  !0 = $requestResult, !1 = $decodedResult, !2 = $xmlElement
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   ASSIGN                                                   !0, '%7B%0A++++%22name%22%3A+%22Benjamin+Clementine%22%2C%0A++++%22mbid_id%22%3A+%22578e5d1c-be21-4158-b147-30aad67f207d%22%2C%0A++++%22albums%22%3A+%7B%0A++++++++%2268e65535-f133-408d-93dd-205e9555eaa5%22%3A+%7B%0A++++++++++++%22albumcover%22%3A+%5B%0A++++++++++++++++%7B%0A++++++++++++++++++++%22id%22%3A+%22172070%22%2C%0A++++++++++++++++++++%22url%22%3A+%22http%3A%2F%2Fassets.fanart.tv%2Ffanart%2Fmusic%2F578e5d1c-be21-4158-b147-30aad67f207d%2Falbumcover%2Fat-least-for-now-55b74dfa59411.jpg%22%2C%0A++++++++++++++++++++%22likes%22%3A+%220%22%0A++++++++++++++++%7D%0A++++++++++++%5D%0A++++++++%7D%0A++++%7D%0A%7D'
   40     1        INIT_FCALL                                               'json_decode'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 <true>
          4        DO_ICALL                                         $4      
          5        ASSIGN                                                   !1, $4
   41     6        INIT_FCALL                                               'arraytoxml'
          7        SEND_VAR                                                 !1
          8        DO_FCALL                                      0  $6      
          9        ASSIGN                                                   !2, $6
   43    10        INIT_FCALL                                               'var_dump'
         11        INIT_METHOD_CALL                                         !2, 'xpath'
         12        SEND_VAL_EX                                              '%2Froot%2Falbums%2F%2A%2Falbumcover%2F%2A%2Furl%5B1%5D'
         13        DO_FCALL                                      0  $8      
         14        FETCH_DIM_R                                      ~9      $8, 0
         15        FETCH_DIM_R                                      ~10     ~9, 0
         16        SEND_VAL                                                 ~10
         17        DO_ICALL                                                 
         18      > RETURN                                                   1

Function arraytoxml:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 15
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 11
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
2 jumps found. (Code = 77) Position 1 = 16, Position 2 = 34
Branch analysis from position: 16
2 jumps found. (Code = 78) Position 1 = 17, Position 2 = 34
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 29
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 33
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
Branch analysis from position: 11
2 jumps found. (Code = 77) Position 1 = 16, Position 2 = 34
Branch analysis from position: 16
Branch analysis from position: 34
Branch analysis from position: 15
filename:       /in/rn6Km
function name:  arrayToXml
number of ops:  37
compiled vars:  !0 = $array, !1 = $rootElement, !2 = $xml, !3 = $_xml, !4 = $v, !5 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      null
          2        RECV_INIT                                        !2      null
    5     3        ASSIGN                                                   !3, !2
    7     4        TYPE_CHECK                                    2          !3
          5      > JMPZ                                                     ~7, ->15
    8     6    >   NEW                                              $8      'SimpleXMLElement'
          7        TYPE_CHECK                                  1020          !1
          8      > JMPZ                                                     ~9, ->11
          9    >   QM_ASSIGN                                        ~10     !1
         10      > JMP                                                      ->12
         11    >   QM_ASSIGN                                        ~10     '%3Croot%2F%3E'
         12    >   SEND_VAL_EX                                              ~10
         13        DO_FCALL                                      0          
         14        ASSIGN                                                   !3, $8
   11    15    > > FE_RESET_R                                       $13     !0, ->34
         16    > > FE_FETCH_R                                       ~14     $13, !4, ->34
         17    >   ASSIGN                                                   !5, ~14
   12    18        TYPE_CHECK                                  128          !4
         19      > JMPZ                                                     ~16, ->29
   13    20    >   INIT_FCALL_BY_NAME                                       'arrayToXml'
         21        SEND_VAR_EX                                              !4
         22        SEND_VAR_EX                                              !5
         23        INIT_METHOD_CALL                                         !3, 'addChild'
         24        SEND_VAR_EX                                              !5
         25        DO_FCALL                                      0  $17     
         26        SEND_VAR_NO_REF_EX                                       $17
         27        DO_FCALL                                      0          
         28      > JMP                                                      ->33
   15    29    >   INIT_METHOD_CALL                                         !3, 'addChild'
         30        SEND_VAR_EX                                              !5
         31        SEND_VAR_EX                                              !4
         32        DO_FCALL                                      0          
   11    33    > > JMP                                                      ->16
         34    >   FE_FREE                                                  $13
   19    35      > RETURN                                                   !3
   20    36*     > RETURN                                                   null

End of function arraytoxml

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
173.51 ms | 1403 KiB | 18 Q