3v4l.org

run code in 300+ PHP versions simultaneously
<?php // First of all, I've added <root> element to your XML document, // because otherwise it's invalid. // But it's not important for the rest of the code. // // Also I've added additional <somethingelse> tag to show that filtering is working $xmlString = '<root><details> <name>name1</name> <address>address1</address> </details> <details> <name>name2</name> <mobile>mobileNum</mobile> <address>address2</address> <somethingelse>This will be filtered</somethingelse> </details></root>'; $xml = new SimpleXMLElement($xmlString); //array_flip to get node names as keys for later foreach loop $nodes = array_flip(array ( 'person_name' => 'name', 'mobile_no' => 'mobile', 'address' => 'address', )); $final_data = array(); //Here are all <details> sections' data in array. $node_values = $xml->xpath('//details'); $node_values = json_decode(json_encode((array)$node_values), TRUE); //this loop filters XML data from keys not existing in $nodes, which are the only that you want to keep foreach($node_values as $node) { $final_data[] = array_intersect_key($node, $nodes); } var_dump($final_data);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 24, Position 2 = 32
Branch analysis from position: 24
2 jumps found. (Code = 78) Position 1 = 25, Position 2 = 32
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 32
filename:       /in/ICO5i
function name:  (null)
number of ops:  37
compiled vars:  !0 = $xmlString, !1 = $xml, !2 = $nodes, !3 = $final_data, !4 = $node_values, !5 = $node
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   ASSIGN                                                   !0, '%3Croot%3E%3Cdetails%3E%0A++++++++%3Cname%3Ename1%3C%2Fname%3E%0A++++++++%3Caddress%3Eaddress1%3C%2Faddress%3E%0A++++%3C%2Fdetails%3E%0A++++%3Cdetails%3E%0A++++++++%3Cname%3Ename2%3C%2Fname%3E%0A++++++++%3Cmobile%3EmobileNum%3C%2Fmobile%3E%0A++++++++%3Caddress%3Eaddress2%3C%2Faddress%3E%0A++++++++%3Csomethingelse%3EThis+will+be+filtered%3C%2Fsomethingelse%3E%0A++++%3C%2Fdetails%3E%3C%2Froot%3E'
   20     1        NEW                                              $7      'SimpleXMLElement'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !1, $7
   22     5        INIT_FCALL                                               'array_flip'
   24     6        SEND_VAL                                                 <array>
   22     7        DO_ICALL                                         $10     
          8        ASSIGN                                                   !2, $10
   29     9        ASSIGN                                                   !3, <array>
   32    10        INIT_METHOD_CALL                                         !1, 'xpath'
         11        SEND_VAL_EX                                              '%2F%2Fdetails'
         12        DO_FCALL                                      0  $13     
         13        ASSIGN                                                   !4, $13
   33    14        INIT_FCALL                                               'json_decode'
         15        INIT_FCALL                                               'json_encode'
         16        CAST                                          7  ~15     !4
         17        SEND_VAL                                                 ~15
         18        DO_ICALL                                         $16     
         19        SEND_VAR                                                 $16
         20        SEND_VAL                                                 <true>
         21        DO_ICALL                                         $17     
         22        ASSIGN                                                   !4, $17
   36    23      > FE_RESET_R                                       $19     !4, ->32
         24    > > FE_FETCH_R                                               $19, !5, ->32
   37    25    >   INIT_FCALL                                               'array_intersect_key'
         26        SEND_VAR                                                 !5
         27        SEND_VAR                                                 !2
         28        DO_ICALL                                         $21     
         29        ASSIGN_DIM                                               !3
         30        OP_DATA                                                  $21
   36    31      > JMP                                                      ->24
         32    >   FE_FREE                                                  $19
   40    33        INIT_FCALL                                               'var_dump'
         34        SEND_VAR                                                 !3
         35        DO_ICALL                                                 
         36      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
142.2 ms | 1446 KiB | 18 Q