PHP XML Parsing Erases Attribute Value -
i have following code below:
<?php $xml = '<divisions xmlns="urn:description7a.services.chrome.com"> <responsestatus responsecode="successful" description="successful"/> <division id="1">acura</division> <division id="44">aston martin</division> <division id="4">audi</division> <division id="45">bentley</division> <division id="5">bmw</division> <division id="6">buick</division> <division id="7">cadillac</division> <division id="8">chevrolet</division> <division id="9">chrysler</division> <division id="11">dodge</division> <division id="46">ferrari</division> <division id="59">fiat</division> <division id="13">ford</division> <division id="170">freightliner</division> <division id="15">gmc</division> <division id="16">honda</division> <division id="17">hyundai</division> <division id="18">infiniti</division> <division id="19">isuzu</division> <division id="20">jaguar</division> <division id="21">jeep</division> <division id="22">kia</division> <division id="47">lamborghini</division> <division id="23">land rover</division> <division id="24">lexus</division> <division id="25">lincoln</division> <division id="48">lotus</division> <division id="53">maserati</division> <division id="26">mazda</division> <division id="61">mclaren</division> <division id="27">mercedes-benz</division> <division id="2">mini</division> <division id="29">mitsubishi</division> <division id="30">nissan</division> <division id="34">porsche</division> <division id="57">ram</division> <division id="49">rolls-royce</division> <division id="52">scion</division> <division id="42">smart</division> <division id="37">subaru</division> <division id="56">tesla</division> <division id="39">toyota</division> <division id="40">volkswagen</division> <division id="41">volvo</division> </divisions> '; $parse = simplexml_load_string($xml); print_r($parse);
the problem is it's not keeping attribute values in xml like
<division id="1">acura</division> <division id="44">aston martin</division>
the php erasing like:
array ( 0 => 'acura', 1 => 'aston martin' );
i'm passing json need stay in format of array, need indexes value of division id attribute. it's outputting right now:
simplexmlelement object ( [responsestatus] => simplexmlelement object ( [@attributes] => array ( [responsecode] => successful [description] => successful ) ) [division] => array ( [0] => acura [1] => aston martin [2] => audi [3] => bentley [4] => bmw [5] => buick [6] => cadillac [7] => chevrolet [8] => chrysler [9] => dodge [10] => ferrari [11] => fiat [12] => ford [13] => freightliner [14] => gmc [15] => honda [16] => hyundai [17] => infiniti [18] => isuzu [19] => jaguar [20] => jeep [21] => kia [22] => lamborghini [23] => land rover [24] => lexus [25] => lincoln [26] => lotus [27] => maserati [28] => mazda [29] => mclaren [30] => mercedes-benz [31] => mini [32] => mitsubishi [33] => nissan [34] => porsche [35] => ram [36] => rolls-royce [37] => scion [38] => smart [39] => subaru [40] => tesla [41] => toyota [42] => volkswagen [43] => volvo ) )
thank help!
simplexml taking group of <division>
elements , creating array out of them , assigning indexes sees fit.
what can nest <id></id>
, <name></name>
tag within each <division></division>
element , foreach
way through new array of simplexmlobjects
, data way.
so if have xml code looks this:
<divisions> <division> <id>16</id> <name>honda</name> </divsion> <division> <id>45</id> <name>bently</name> </division> <division> <id>1</id> <name>acura</name> </division> </divisions>
you process this:
$xml = simplexml_load_file("divisions.xml"); $divisions = array(); foreach($xml $division) { $divisions[(int)$division->id] = (string)$division->name; } var_dump($divisions); //var dump produce array of... array (size=3) 16 => string 'honda' (length=5) 45 => string 'bently' (length=6) 1 => string 'acura' (length=5)
that put of indexes in right place.
Comments
Post a Comment