Skip to content

Instantly share code, notes, and snippets.

@tablatronix
Last active April 3, 2018 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tablatronix/942e04076901a4c561ef47f07f40695a to your computer and use it in GitHub Desktop.
Save tablatronix/942e04076901a4c561ef47f07f40695a to your computer and use it in GitHub Desktop.
cannot print xml sub collection
<?php
// why does print_r($xmlobj->node) only print the first node ?
$string = <<<XML
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>
Two of our famous Belgian Waffles with plenty of real maple syrup
</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>
Light Belgian waffles covered with strawberries and whipped cream
</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>
Light Belgian waffles covered with an assortment of fresh berries and whipped cream
</description>
<calories>900</calories>
</food>
</breakfast_menu>
XML;
$xml = simplexml_load_string($string);
$xmlfood = $xml->food;
print_r(count($xmlfood));
print_r("<br/>\n");
print_r(gettype($xmlfood));
print_r("<br/>\n");
// prints entire xml
# print_r($xmlfood);
# print_r("<br/>\n");
// prints only 1 node
print_r($xmlfood);
print_r("<br/>\n");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment