Haighy Posted October 3, 2012 Share Posted October 3, 2012 i am parsing through xml and want to know when i am at the end of a given 'Product' object ie when at </Product> he is my code $ParsedXml = simplexml_load_string($response); foreach($ParsedXml->Product as $current)( if (isset($current->name)) ( $name = $current->name, ) ...etc... ) I want to know when i am at the end of that Product object so i can write a line into a dabase before going through to the next one Link to comment Share on other sites More sharing options...
boen_robot Posted October 3, 2012 Share Posted October 3, 2012 Using the count(), you can get the total number of child elements of a particular node (e.g. all elements inside "Product"). In addition to that, at the foreach, you can request getting the posistion of the current node as a key foreach($ParsedXml->Product as $position => $current) Within the foreach, simply check if ($position < $count), and act accordingly. Link to comment Share on other sites More sharing options...
Haighy Posted October 3, 2012 Author Share Posted October 3, 2012 the number of child nodes is variable Link to comment Share on other sites More sharing options...
boen_robot Posted October 3, 2012 Share Posted October 3, 2012 Errr.... is that a question or statement?If statement... yes... so what? count() would detect that number.If it's a question... come on, do I need to spell out the code? $count = $ParsedXml->count();foreach($ParsedXml->Product as $position => $current) {if ($position < $count) {//Last node}} Link to comment Share on other sites More sharing options...
Haighy Posted October 3, 2012 Author Share Posted October 3, 2012 it is a statement so that count surely would not work as i don't know how many child nodes exist. the number is dynamic Link to comment Share on other sites More sharing options...
boen_robot Posted October 3, 2012 Share Posted October 3, 2012 If you knew, you'd just write the number. You need to use the count() function instead exactly becase you don't know, and the number is dynamic. Link to comment Share on other sites More sharing options...
Haighy Posted October 4, 2012 Author Share Posted October 4, 2012 ahh sorry - I understand now. I did not see this line in your example $count = $ParsedXml->count(); Is it possible to put this count statement inside the foreach loop? each Product could have a different number of child nodes Link to comment Share on other sites More sharing options...
Haighy Posted October 4, 2012 Author Share Posted October 4, 2012 actually have found the solution thanks to your kind assistence i used this inside the foreach $NodeCount = $current ->count(); thanks again Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now