newbie question for XML.. why array == 1st element

This is a discussion on newbie question for XML.. why array == 1st element within the PHP Language forums, part of the PHP Programming Forums category; it seems quite strange that the array also prints out as element 1 in the array in the following: the ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008
Summercool
 
Posts: n/a
Default newbie question for XML.. why array == 1st element

it seems quite strange that the array also prints out as element 1 in
the array in the following:

the second print_r() statement... printing out an array actually
prints out the first element...

Code:
================================================== ======================
<pre>
<?php

$xml =<<<XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<notes>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget this weekend!</body>
</note>
<note>
<to>Tove2</to>
<from>Jani2</from>
<heading>Reminder2</heading>
<body>Don't forget this weekend2!</body>
</note>
</notes>

XML;

$xml = new SimpleXMLElement($xml);

print_r($xml);
print_r($xml->note);
print_r($xml->note[0]);
print_r($xml->note[1]);

echo "\n\nlooping\n\n";
foreach($xml->note as $note)
print_r($note);

?>


Output:
================================================== ======================
SimpleXMLElement Object
(
[note] => Array
(
[0] => SimpleXMLElement Object
(
[to] => Tove
[from] => Jani
[heading] => Reminder
[body] => Don't forget this weekend!
)

[1] => SimpleXMLElement Object
(
[to] => Tove2
[from] => Jani2
[heading] => Reminder2
[body] => Don't forget this weekend2!
)

)

)
SimpleXMLElement Object
(
[to] => Tove
[from] => Jani
[heading] => Reminder
[body] => Don't forget this weekend!
)
SimpleXMLElement Object
(
[to] => Tove
[from] => Jani
[heading] => Reminder
[body] => Don't forget this weekend!
)
SimpleXMLElement Object
(
[to] => Tove2
[from] => Jani2
[heading] => Reminder2
[body] => Don't forget this weekend2!
)


looping

SimpleXMLElement Object
(
[to] => Tove
[from] => Jani
[heading] => Reminder
[body] => Don't forget this weekend!
)
SimpleXMLElement Object
(
[to] => Tove2
[from] => Jani2
[heading] => Reminder2
[body] => Don't forget this weekend2!
)


------------------------------------------------------------------------
if you are egoistic and self-righteous, thinking any other behavior
unlike your own is stupid, please do not reply to this post, or
your posting will be ignored.
------------------------------------------------------------------------
Reply With Quote
  #2 (permalink)  
Old 03-01-2008
petersprc
 
Posts: n/a
Default Re: newbie question for XML.. why array == 1st element

Hi,

This is because simple XML nodes are stored internally as objects in a
linked list.

When you use the arrow (->) operator, the first matching child is
returned by the read_property object handler.

When all of a node's properties are fetched as a result of executing
print_r, children with the same name are combined into an array by the
get_properties object handler.

SimpleXMLElement implements the Travesable interface, which allows you
to iterate over these objects using foreach.

More info on object handler functions is here:
http://www.php-mag.net/magphpde/magp...nodeid,21.html

Regards,

John Peters

On Feb 29, 10:16 pm, Summercool <Summercooln...@gmail.com> wrote:
> it seems quite strange that the array also prints out as element 1 in
> the array in the following:
>
> the second print_r() statement... printing out an array actually
> prints out the first element...
>
> Code:
> ================================================== ======================
> <pre>
> <?php
>
> $xml =<<<XML
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <notes>
> <note>
> <to>Tove</to>
> <from>Jani</from>
> <heading>Reminder</heading>
> <body>Don't forget this weekend!</body>
> </note>
> <note>
> <to>Tove2</to>
> <from>Jani2</from>
> <heading>Reminder2</heading>
> <body>Don't forget this weekend2!</body>
> </note>
> </notes>
>
> XML;
>
> $xml = new SimpleXMLElement($xml);
>
> print_r($xml);
> print_r($xml->note);
> print_r($xml->note[0]);
> print_r($xml->note[1]);
>
> echo "\n\nlooping\n\n";
> foreach($xml->note as $note)
> print_r($note);
>
> ?>
>
> Output:
> ================================================== ======================
> SimpleXMLElement Object
> (
> [note] => Array
> (
> [0] => SimpleXMLElement Object
> (
> [to] => Tove
> [from] => Jani
> [heading] => Reminder
> [body] => Don't forget this weekend!
> )
>
> [1] => SimpleXMLElement Object
> (
> [to] => Tove2
> [from] => Jani2
> [heading] => Reminder2
> [body] => Don't forget this weekend2!
> )
>
> )
>
> )
> SimpleXMLElement Object
> (
> [to] => Tove
> [from] => Jani
> [heading] => Reminder
> [body] => Don't forget this weekend!
> )
> SimpleXMLElement Object
> (
> [to] => Tove
> [from] => Jani
> [heading] => Reminder
> [body] => Don't forget this weekend!
> )
> SimpleXMLElement Object
> (
> [to] => Tove2
> [from] => Jani2
> [heading] => Reminder2
> [body] => Don't forget this weekend2!
> )
>
> looping
>
> SimpleXMLElement Object
> (
> [to] => Tove
> [from] => Jani
> [heading] => Reminder
> [body] => Don't forget this weekend!
> )
> SimpleXMLElement Object
> (
> [to] => Tove2
> [from] => Jani2
> [heading] => Reminder2
> [body] => Don't forget this weekend2!
> )
>
> ------------------------------------------------------------------------
> if you are egoistic and self-righteous, thinking any other behavior
> unlike your own is stupid, please do not reply to this post, or
> your posting will be ignored.
> ------------------------------------------------------------------------


Reply With Quote
  #3 (permalink)  
Old 03-01-2008
Summercool
 
Posts: n/a
Default Re: newbie question for XML.. why array == 1st element

On Feb 29, 11:51 pm, petersprc <peters...@gmail.com> wrote:
> Hi,
>
> This is because simple XML nodes are stored internally as objects in a
> linked list.
>
> When you use the arrow (->) operator, the first matching child is
> returned by the read_property object handler.
>
> When all of a node's properties are fetched as a result of executing
> print_r, children with the same name are combined into an array by the
> get_properties object handler.
>
> SimpleXMLElement implements the Travesable interface, which allows you
> to iterate over these objects using foreach.
>
> More info on object handler functions is here:http://www.php-mag.net/magphpde/magp...,id,382,nodeid...



that's interesting... so in other words... if i pass $xml->note to a
function, the memory of exactly 1 node is passed.

in other language... a linked list will have a member variable $next
that explicitly points to the next member...

so in PHP, it looks like a node, without having a $next... and yet it
is a linked list.

and print_r will print it as an Array type.

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 11:59 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0