Hello,

For me it is weird this thing:

Let say there is a part of xml:

<result comments="LAST :T3 DIST : 3/4" favouritenews="WINNER & T3" id="3131846" message="OFF" 

nonrunners="0" ran="6" settlingstatus="D" statuscode="O">
<position fav="jf" id="8669343" name="Elizas Impact" num="1" photo="N" position="1" runnernumber="4" 

selectionref="26973474" sp="11/4" winnersTime="00:00:28.920"/>
<position id="8669348" name="Amazing Madam" num="2" photo="N" position="2" runnernumber="6" 

selectionref="26973476" sp="9/2" winningdistance="3/4"/>
<position deadheat="A" id="8669349" name="Ardera Nelson" num="3" photo="N" position="3" 

runnernumber="1" selectionref="26973471" sp="4/1" winningdistance="3/4"/>
<position deadheat="B" id="8669350" name="Borna Trumpet" num="4" photo="N" position="3" 

runnernumber="2" selectionref="26973472" sp="3/1" winningdistance="DHT"/>
</result>

I load this to the object with this function - simplexml_load_string

When I do:

print_r($event->result->position);

it prints only the first position tag. BUt if I would do

print_r($event->result);

I think it would look as array of positions.

And when I do this:

 $winners = $this->get_winners($event->result->position);

function get_winners() {
 foreach ($position as $pos) {
    // runs trhoug all positions - how is that posible?
 }
}

It is actincg as array. So runs thorug all positions as in xml.

So then why I cannot access all positions with

print_r($event->result->position);

but only first one? I did bit of googling - and it migt have something to do with iterators. But then print_r function does not pirnt out any information about those iterators. How can I know when they are existing, which not and so on.

I mean by experimenting I found out this weirdness, but when programming it would be good to know exacly why things are such. Even for my co-worker when I showed that - was weird.

I think because result is an iterator, calling ->position just returns the current item, which you can move with next or rewind. Because Iterator is implemented as an interface, print_r (old function) has no way to detect this. Outputting result just calls the Iterator showing all position objects.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.