Solved

PHP SimpleXML foreach loop not looping

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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
P3P validator , ASP, PHP ,Https and Http links 3 45
What is key combo for a b with a forward slash through it? 12 66
Joomla Editing 3 23
Position image fpdf 4 19
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

752 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