Solved

PHP SimpleXML foreach loop not looping

Posted on 2014-02-16
2
892 Views
Last Modified: 2014-02-16
Hi,

I have a rather odd problem relating to a XML lookup and the foreach loop not actually looping.

The XML data I am trying to read is ask follows:

<bullets>
		<bullet id="1">
			Item A
		</bullet>
		<bullet id="2">
			Item B
		</bullet>
		<bullet id="3">
			Item C
		</bullet>
		<bullet id="4">
			Item D
		</bullet>
	</bullets>

Open in new window


This extract comes from a larger file under the main section <details>.

So my loop is setup as follows:

$details = simplexml_load_file("some.xml");

foreach($details->bullets as $bullets) {
		
		echo $bullets->bullet;
	}

Open in new window


All that does however is output the 1st "Item A", nothing else.

If I do the following then it outputs: 4 as I would expect as there are 4 items.

foreach($details->bullets as $bullets) {
		// DO NOTHING
	}

echo count($bullets);

Open in new window


Am I missing something? This has worked perfectly in other sections of the file, although there have been other values contained within the particular tag in those cases.

Thanks.
0
Comment
Question by:Lee Redhead
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 

Accepted Solution

by:
Lee Redhead earned 0 total points
ID: 39863473
I solved it, needed to do the following:

foreach($details->bullets->bullet as $bullet) {
// LOOP CODE
}

Open in new window


As there are no sub-tags then there was nothing to loop through.
0
 

Author Closing Comment

by:Lee Redhead
ID: 39863477
Silly mistake made originally with the lookup resulting it only one item to loop.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question