PHP, SimpleXML arrays. Unanticipated casting of an array to a string

th3criminal

New Member
Sample XML:\[code\]<root> <ratings>3</ratings> <ratings>5</ratings> <ratings>7</ratings></root>\[/code\]The following code is the basis for my small application, it works as would be expected:\[code\]<?php // $xml is some simplexml object sizeof($xml->ratings); //3 foreach($xml->ratings as $rating){ echo($rating->value."::"); //this echoes all 3 rating values: 3::5::7}?>\[/code\]This next code, which I would normally consider to be equivalent is not. And I have no idea why:\[code\]<?php // $xml is some simplexml object $ratings = $xml->ratings; sizeof($ratings); //3, all is well so far foreach($ratings as $rating){ echo($rating."::"); /*this echoes a never-ending list of ratings, looking like 3::5::5::5::5::5::5::5...... */ }?>\[/code\]My feeling is that the assignment operator is casting the array of simplexml objects (ratings objects) as something odd, but have no clue as to how.Any ideas?Other little hints:\[code\]var_dump($xml);/* Output is:object(SimpleXMLElement)#7 (1) { ["ratings"]=> array(3) { [0]=> string(1) "3" [1]=> string(1) "5" [2]=> string(1) "7" }}*/var_dump($ratings);/* Output is:object(SimpleXMLElement)#6 (1) { [0]=> string(1) "3"}*/\[/code\]
 
Back
Top