Accessing a XML node directly via a key

gladyscrow

New Member
\[code\]$xml = simplexml_load_file($xmlPath);$items = $xml->list->item;...echo $items[$currentIndex]->asXML();\[/code\]When I print out $currentIndex on each iteration, I get 0, 1, 2, 3, 4, etc.When I hard code $items[0]->asXML(); $items[1]->asXML(); $items[2]->asXML(); etc. I get the data I want.But when I loop like I do in the first code segment, it prints out items 0, 2, 4, etc.How is that possible and what could be causing this?Thanks,RyanADDED INFO:This is the main part of it:\[code\]$totalItems = 45;$keepItems = 10;$currentIndex = 0;while($totalItems > $keepItems){ $totalItems -= 1; print_r($xml->list->item[$currentIndex]); $currentIndex += 1;}\[/code\]I just tried this in a separate file and it worked in that instance:\[code\]$xml = simplexml_load_file($xmlPath);$items = $xml->list->item;$counter = 45;$display = 0;while($counter > 4){ echo $items[$display]->asXML(); $display += 1; $counter -= 1;}\[/code\]So something in my other code is making this happen. I will have to look at it some more as well, but it's for sure nothing obvious.Thanks,RyanADDED INFO 2:OK, I determined the line of code that causes this "every other one" syndrome :)\[code\]unset($items[$currentIndex]);\[/code\]The thought was to remove/unset an item once I used the data, but it doesn't seem to work the way I expected -- does anybody have an idea of why? Why is it unsetting something it hasn't displayed?Thanks,Ryan
 
Back
Top