php - How have foreach loop with this simplexml_load_string -
i parsing xml using simplexml_load_string :
$xml = simplexml_load_string($response); echo '{'. '"date":"'.$xml->date[0].'",'. '"description":"'.$xml->shiptos->shipto->shippinggroups->shippinggroup->orderitems->orderitem->description[0].'",'. '"track":"'.$xml->shipments->shipment->track[0].'"'. '}';
this works ok if node appears in xml multiple times grabs once. can please me understand how write foreach loop description node?
you referring 1 instance of each simplexmlobject
. example $xml->date[0]
refers first occurence of date object only. print date objects need loop through them
foreach( $xml->date $date ){ print (string)$date; }
alternatively, use children
function:
foreach( $xml->children('date') $date ){ print (string)$date; }
Comments
Post a Comment