I'm having trouble converting XML into an Array

JacklynCapoa

New Member
I'm a novice in PHP so go easy on me ;)Basically I have an XML file which I'm trying to convert into an Array using PHP. My XML file, supplies.xml, looks a bit like this...\[code\]<Supplies> <supply name="Pen"> <supplier name="Pen Island">http://domain.com/</supplier> <quantity>2000</quantity> <cost>100.00</cost> </supply> <supply name="Pencil"> <supplier name="Stationary World">http://domain.com/</supplier> <quantity>5000</quantity> <cost>115.30</cost> </supply> <supply name="Paper"> <supplier name="Stationary World">http://domain.com/</supplier> <quantity>100</quantity> <cost>10.50</cost> </supply></Supplies>\[/code\]I want it converting into an Array with this structure...\[code\]Array ( [Pen] => Array ( [supplier] => Pen Island [supplier_link] => http://domain.com/ [quantity] => 2000 [cost] => 100 ) [Pencil] => Array ( [supplier] => Stationary World [supplier_link] => http://domain.com/ [quantity] => 5000 [cost] => 115.3 ) [Paper] => Array ( [supplier] => Stationary World [supplier_link] => http://domain.com/ [quantity] => 100 [cost] => 10.5 ))\[/code\]I tried this but PHP doesn't like it...\[code\]<?php $xml_supplies = simplexml_load_file("supplies.xml"); $supplies = array(); foreach ($xml_supplies->Supplies->supply as $supply) { $supplies[(string)$supply['name']] = array( "supplier" => (string)$supply->supplier['name'], "supplier_link" => (string)$supply->supplier, "quantity" => (int)$supply->quantity, "cost" => (float)$supply->cost ) } print_r($supplies);?>\[/code\]My theory behind it was that it would loop though each supply element and add to the $supplies Array.I've spent nearly an hour trying to get it to work but I've given up and would like some help. Thanks.
 
Top