Jump to content
mouseywings

PHP XML Attributes Parsing Resets into Array

Recommended Posts

I have the 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 is that it's not keeping the attribute values in the xml like

 



    <division id="1">Acura</division>
    <division id="44">Aston Martin</division>


 

The PHP is erasing it to be like:

 



    array (
      0 => 'Acura',
      1 => 'Aston Martin'
    );


 

I'm passing this to JSON so I need it to stay in the format of an array, but need the indexes to be the value of the division ID attribute. This is what it's exactly 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 you for any help!

Share this post


Link to post
Share on other sites

try

$data = array();
    foreach ($parse->division as $div) {
        $key = (string)$div['id'];
        $val = (string)$div;
        $data[$key] = $val;
    }

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.