php - Weird behaviour in SimpleXMLElement Object when printing the array -


i'm struggling array in simplexmlelement object. somehow don't expected result when print array $node->reference.

print_r($node); shows:

simplexmlelement object (     [reference] => array         (             [0] => simplexmlelement object                 (                     [@attributes] => array                         (                             [resourceidentifier] => 52chgb7f-1a00-4eaf-ac8a-5d4557f9796a                         )                  )              [1] => simplexmlelement object                 (                     [@attributes] => array                         (                             [resourceidentifier] => 52cbccc3-b754-4e88-9238-5d5257f9796a                         )                  )          ) ) 

but print_r($node->reference); , print_r($node->reference->children()); shows:

simplexmlelement object (     [@attributes] => array         (             [resourceidentifier] => 52chgb7f-1a00-4eaf-ac8a-5d4557f9796a         )  ) 

i expect see:

array (     [0] => simplexmlelement object         (             [@attributes] => array                 (                     [resourceidentifier] => 52chgb7f-1a00-4eaf-ac8a-5d4557f9796a                 )          )      [1] => simplexmlelement object         (             [@attributes] => array                 (                     [resourceidentifier] => 52cbccc3-b754-4e88-9238-5d5257f9796a                 )          )  ) 

edit

here code reproduce:

<?php  $xml = '<?xml version="1.0" encoding="utf-8" ?> <items>     <item>         <reference resourceidentifier="52chgb7f-1a00-4eaf-ac8a-5d4557f9796a" />         <reference resourceidentifier="52cbccc3-b754-4e88-9238-5d5257f9796a" />     </item>     <item>         <reference resourceidentifier="52chgb7f-1a00-4eaf-ac8a-5d4557f9796a" />     </item>     <item>         <reference resourceidentifier="52chgb7f-1a00-4eaf-ac8a-5d4557f9796a" />         <reference resourceidentifier="52chgb7f-1a00-4eaf-ac8a-5d4557f9796a" />         <reference resourceidentifier="52cbccc3-b754-4e88-9238-5d5257f9796a" />     </item> </items>';  $items = new \simplexmlelement($xml); foreach ($items $item) {      echo '<h1>item</h1>';     echo '<pre>';     print_r($item);     print_r($item->reference); // returns 1 simplexmlelement object?     print_r($item->reference->children()); // returns 1 simplexmlelement object?     echo '</pre>';  } 

the simple answer is: don't rely on print_r. thing simplexml uses lot of, want of better word, "magic", , print_r (and var_dump, var_export, , pretty other generic debug or serialize function) doesn't show how behave. also, , important, a simplexmlelement not contain arrays.

i wrote a dedicated debug function which, while not perfect, better job of recursing through simplexml objects native ones.

the reason specific behaviour can use $node->reference refer either list of children called reference, or first such child. following equivalent:

// access iterable list foreach ( $node->reference $ref ) {     echo $ref['resourceidentifier'];     // loop once     break; }  // access numerically indexed array echo $node->reference[0]['resourceidentifier'];  // access first item default echo $node->reference['resourceidentifier']; 

this extremely handy when have document "deep narrow", e.g.

$xml = simplexml_load_string('<foo><bar><baz><quux hello="world" /></baz></bar></foo>'); echo $xml->bar->baz->quux['hello']; // world 

rather having check whether node unique or multiple, simplexml lets write such expression , ignore multiples:

$xml = simplexml_load_string('<foo><bar><baz><quux hello="world" /><quux ignored="true" /></baz></bar><bar>ignored</bar></foo>'); echo $xml->bar->baz->quux['hello']; // world 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -