Solved

PHP SimpleXML foreach loop not looping

Posted on 2014-02-16
2
791 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
  • 2
2 Comments
 

Accepted Solution

by:
Lee Redhead earned 0 total points
Comment Utility
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
Comment Utility
Silly mistake made originally with the lookup resulting it only one item to loop.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now