473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Array length in SimpleXML

This is idiotic, but I can't figure this out:

I can do this:
$xml = new SimpleXMLElement('<items>
<item><name>Lee</name><count>1</count></item>
<item><name>Bill</name><count>5</count></item></items>');
foreach ($xml->item as $item) {
echo $item->name, '<br />';
}

But I can't figure how to tell I have 2 items. The obvious:

print count($xml->item); // same for $xml->items
and
print $xml->item->length; // same for $xml->items->length

They don't work the way I expect..

This is my first foray into XML, so I'm sure I'm doing something
stupid.. would anyone mind pointing it out?

Thanks,

Lee

Nov 24 '06 #1
1 4947
Hi,

Try maybe:

echo "There are " . count($xml->xpath('/items/item')) . " top-level
items.\n";

lh*****@gmail.com wrote:
This is idiotic, but I can't figure this out:

I can do this:
$xml = new SimpleXMLElement('<items>
<item><name>Lee</name><count>1</count></item>
<item><name>Bill</name><count>5</count></item></items>');
foreach ($xml->item as $item) {
echo $item->name, '<br />';
}

But I can't figure how to tell I have 2 items. The obvious:

print count($xml->item); // same for $xml->items
and
print $xml->item->length; // same for $xml->items->length

They don't work the way I expect..

This is my first foray into XML, so I'm sure I'm doing something
stupid.. would anyone mind pointing it out?

Thanks,

Lee
Nov 25 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: hwcowan | last post by:
Hello, I have started using SimpleXML and can do most things, but there are a couple of things that I can't seem to figure out. Currently, I can: 1. Open & load an XML file 2. Manually...
3
by: theboss3 | last post by:
Take, for example a typical RSS 2.0 feed (like http://lorelle.wordpress.com/feed/) and tell me how I access the tags like 'content:encoded' and 'wfw:commentRSS' using SimpleXML. I've searched far...
2
by: mandric | last post by:
Hello, Can someone please enlighten me on how to preserve the <!]> element when parsing an xml file or string with simplexml. I'm using libxml 2.6.16 and php 5.1.4. I tried a few variations,...
1
by: Andy | last post by:
Hi, I'm running in to a little issue with SimpleXML, and wondered if anyone knew if it was normal implementation for SimpleXML, or I'm not using a correct flag, or even it's a an issue that...
1
by: Chuy08 | last post by:
Currently I have the following XML document: <Programs> <Program> <ProductTypeName>30 Yr Fixed</ProductTypeName> <Rate>6.250</Rate> <APR>6.274</APR> <InvestorName>Helos</InvestorName>...
0
by: emmettnicholas | last post by:
Hi, I am new to PHP, and I'm trying to implement something similar to SimpleXML. The following code demonstrates an example of what I'm trying to achieve: <?php $xmlstr = <<<XML <asdf> <foo...
0
by: ty | last post by:
I have a script that takes a xml template then adds data into it using SimpleXMLElement. I then save it using asXML into a mysql database. On my development machine at home it works fine. ...
3
by: =?ISO-8859-1?Q?Feij=F3?= | last post by:
I've been trying to find a code or a way to create that a parse that return this to me: $html = 'loren ipsum'; $html = 'loren ipsum 2'; $html = 'path/to/a/file'; $html = 'header 1'; $html =...
5
by: KDawg44 | last post by:
Hi, Is there a way to get a multidimensional associative array with the entire result set? I would like to get a an array like this: resultsArray How can I accomplish this? Can I do...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.