Solved

PHP SimpleXML foreach loop not looping

Posted on 2014-02-16
2
805 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 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

20 Experts available now in Live!

Get 1:1 Help Now