473,324 Members | 2,166 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,324 software developers and data experts.

When is an array not an array?

Hi, folks.

I've encountered what seems to me to be something of an oddity while playing
around with XML parsing in PHP, and I wondered if any of you might be able
to clear up my confusion...

Here's a little code:

$xmlDoc = new DOMDocument();
$xmlDoc->load('widget_data.xml');
$widgets = $xmlDoc->getElementsByTagName('widget');

My understanding was that '$widgets' is an array of elements, and the
following 'foreach' iterates through that array (this works):

foreach ($widgets as $widget)
{
....
}

However, I get an error if I try to access '$widgets' using square-brackets,
e.g.:

$widget = $widgets[0];

(The error is: "Fatal error: Cannot use object of type DOMNodeList as
array".)

PHP confirms that the first element of the 'array' has an index/key of '0'
with the following (or similar), but won't let me access it directly using
'$widgets[0]':

foreach ($widgets as $key =$widget)
{
echo("<p>" . $key . "</p>\n");
}

Does anyone have an explanation as to why a DOMNodeList can be accessed like
an array using 'foreach', but won't allow square-brackets to be used? What
exactly *is* a DOMNodeList?

Thanks for any help.

A.

PS. I am aware that DOMNodeLists have an 'item()' method with which nodes
within the list can be referred to via index, but my confusion still stands
as to why I can't simply use '[]'s.
Oct 30 '06 #1
7 8102
On Mon, 30 Oct 2006 14:24:45 GMT, "Andrew C" <no******@totally.made.upwrote:
>I've encountered what seems to me to be something of an oddity while playing
around with XML parsing in PHP, and I wondered if any of you might be able
to clear up my confusion...

Here's a little code:

$xmlDoc = new DOMDocument();
$xmlDoc->load('widget_data.xml');
$widgets = $xmlDoc->getElementsByTagName('widget');

My understanding was that '$widgets' is an array of elements,
Nope - it's a DOMNodeList object, that has support for PHP5 iterators, so you
can use it in a foreach loop.
>and the
following 'foreach' iterates through that array (this works):

foreach ($widgets as $widget)
{
...
}

However, I get an error if I try to access '$widgets' using square-brackets,
e.g.:

$widget = $widgets[0];

(The error is: "Fatal error: Cannot use object of type DOMNodeList as
array".)
There you go.
>PHP confirms that the first element of the 'array' has an index/key of '0'
with the following (or similar), but won't let me access it directly using
'$widgets[0]':

foreach ($widgets as $key =$widget)
{
echo("<p>" . $key . "</p>\n");
}

Does anyone have an explanation as to why a DOMNodeList can be accessed like
an array using 'foreach', but won't allow square-brackets to be used? What
exactly *is* a DOMNodeList?

Thanks for any help.

A.

PS. I am aware that DOMNodeLists have an 'item()' method with which nodes
within the list can be referred to via index, but my confusion still stands
as to why I can't simply use '[]'s.
PHP doesn't support overloading operators, which I believe would be required
for supporting [] on objects implementing the appropriate interface.

Would be a sensible extension to the current functionality, though - as you
point out, DOMNodeList acts almost the same as an array, but not quite close
enough.

Failing that, it'd be somewhat useful if DOMNodeList had a method to convert
itself into an actual array, but it doesn't seem to have that either.

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Oct 30 '06 #2
Does anyone have an explanation as to why a DOMNodeList can be accessed like
an array using 'foreach', but won't allow square-brackets to be used? What
exactly *is* a DOMNodeList?
A DOMNodeList is an object - this is why you can't access items using
square-brackets.
Since PHP5 you can iterate through all visible properties of an object
- this is why you can access items using a foreach statement.

Oct 30 '06 #3
In article <q1********************************@4ax.com>, Andy Hassall
says...
Failing that, it'd be somewhat useful if DOMNodeList had a method to convert
itself into an actual array, but it doesn't seem to have that either.
If, as whiskey says, the nodes are properties of the object, you can do
an explicit cast of the object to an array:

$arrayofnodes = (array)$nodelist;
--
PleegWat
Remove caps to reply
Oct 30 '06 #4

"Andy Hassall" <an**@andyh.co.ukwrote in message
news:q1********************************@4ax.com...
On Mon, 30 Oct 2006 14:24:45 GMT, "Andrew C" <no******@totally.made.up>
wrote:
>>I've encountered what seems to me to be something of an oddity while
playing
around with XML parsing in PHP, and I wondered if any of you might be able
to clear up my confusion...

Here's a little code:

$xmlDoc = new DOMDocument();
$xmlDoc->load('widget_data.xml');
$widgets = $xmlDoc->getElementsByTagName('widget');

My understanding was that '$widgets' is an array of elements,

Nope - it's a DOMNodeList object, that has support for PHP5 iterators, so
you
can use it in a foreach loop.
Thanks. That's my missing piece. I have quite a bit of experience of PHP
prior to 5, but I'm just feeling my way with some of the 5 stuff. :-)

A.
Oct 31 '06 #5
I dare say you can not cast a DOMNodeList to an array. Because, when
converting an object to an array, you get the properties of the object.
And DOMNodeList has only one porperty, which is "length" (and useless
in this context).

Anyway, I can't find any reason why one will ever need to convert a
DOMNodeList to an array since it provides a method to retrieve a node
specified by it's index ("item()"). Moreover, PHP5 offers the ability
to iterate through objects. So why this fuss ?

PleegWat, I never said nodes are properties. All I said was that PHP5
adds the ability to iterate the visible properties of an object. But
that's the default. You can customize the way PHP iterates through
objects by implementing the "Iterator" or "IteratorAggregate"
interfaces ;-)

On Oct 31, 12:27 am, PleegWat
<pleeg...@PLEEGWAT.leegwater-68.demon.nl.INVALIDwrote:
In article <q13ck2d3tc2204gdsi3db9q0bds510p...@4ax.com>, Andy Hassall
says...
Failing that, it'd be somewhat useful if DOMNodeList had a method to convert
itself into an actual array, but it doesn't seem to have that either.If, as whiskey says, the nodes are properties of the object, you can do
an explicit cast of the object to an array:

$arrayofnodes = (array)$nodelist;
--
PleegWat
Remove caps to reply
Oct 31 '06 #6
In article <11**********************@m73g2000cwd.googlegroups .com>,
whiskey says...
On Oct 31, 12:27 am, PleegWat
<pleeg...@PLEEGWAT.leegwater-68.demon.nl.INVALIDwrote:
In article <q13ck2d3tc2204gdsi3db9q0bds510p...@4ax.com>, Andy Hassall
says...
Failing that, it'd be somewhat useful if DOMNodeList had a method to
convert itself into an actual array, but it doesn't seem to have that
either.
If, as whiskey says, the nodes are properties of the object, you can do
an explicit cast of the object to an array:

$arrayofnodes = (array)$nodelist;

I dare say you can not cast a DOMNodeList to an array. Because, when
converting an object to an array, you get the properties of the object.
And DOMNodeList has only one porperty, which is "length" (and useless
in this context).

Anyway, I can't find any reason why one will ever need to convert a
DOMNodeList to an array since it provides a method to retrieve a node
specified by it's index ("item()"). Moreover, PHP5 offers the ability
to iterate through objects. So why this fuss ?

PleegWat, I never said nodes are properties. All I said was that PHP5
adds the ability to iterate the visible properties of an object. But
that's the default. You can customize the way PHP iterates through
objects by implementing the "Iterator" or "IteratorAggregate"
interfaces ;-)
(top-posting fixed)

Sorry, misunderstood you then. I did take a quick look at the manual but
I couldn't find anything about hte DOMNodeList class.

--
PleegWat
Remove caps to reply
Oct 31 '06 #7

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

Similar topics

10
by: Noah Spitzer-Williams | last post by:
Hello guys, I'm itinerating through my array using pointers in this fashion: image is unsigned char image do { cout << "image byte is: " << *image << endl;
1
by: mdub317 | last post by:
I'm totally new to programming and I am wondering; when would be a good time to use an array or an indexer? I want to know what types of applications would make good use of arrays or indexers. ...
2
by: david | last post by:
Well, as a matter of fact I_HAD_MISSED a basic thing or two, anyway, although Ollie's answer makes perfectly sense when dealing with classes, it doesn't seem to me to apply as well if you have to...
13
by: Kantha | last post by:
Hi all, I have declared an Union as follows typedef union { struct interrupt_bits { unsigned char c_int_hs_fs_status : 1, c_setup_intflag : 1,
3
by: ZMan | last post by:
The following code won't compile with gcc version 3.4.2 (mingw-special). How come? Error: cannot convert `char (*)' to `char**' /**********************************************************/...
1
by: DJG79 | last post by:
Hi all, I am using an open source menu that i found and it works great, except for one thing that when the web page is not scrolled to the very top the drop down links will not stay visible. Has...
5
by: comp.lang.php | last post by:
// NEW 11/27/2006: FINALLY, IF YOU ADDED OR DELETED OR DID ANY KIND OF FORM ACTION SUCCESSFULLY, DON'T RE-DISPLAY THE NEW EXPENSE ITEMS VIA $_POST if ($_POST && (!is_array($leaseObj->errorArray)...
5
by: Mercy | last post by:
I guess my C++ is pretty darn rusty. I was just looking over sample C++ code for practice... and I'm kind of confused about this code fragment: int sector2; int i = 3; memset(sector2,...
12
by: lawrence k | last post by:
I've a form that starts off like this: <form method="post" action="profile.php? id=5&formName=my_story_edit.htm" enctype="multipart/form-data"> <p>Email address:<br /> <input type="text"...
1
anfetienne
by: anfetienne | last post by:
i have this code below that i made....it loads vars from txt file splits it then puts it into an array....once in an array it the brings the pics in from the array to create thumbnails and a larger...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.