Jump to content

[SOLVED] xml to array format i want..how to proceed.please help


smilesmita

Recommended Posts

hi there:

i have an xml file which kind of looks like this[as an example i have kept it simple].I want to conver this xml to the array format shown below.dont know how to do it..please help.after i get this array format then it will be easy for me to insert these records into database table by iterating:

<xml version.....>
<SubscriptionEvents>
<subscriptionFile>
  <filename>filename1</filename>
  <statustype>
     <Code>
       <Date>date1</Date>
       <Time>time1</Time>
     </Code>
  <statustype>
  <Manifest>
      <address>address1</address>
  </Manifest>
</subscriptionfile>

<subscriptionFile>
  <filename>filename2</filename>
  <statustype>
     <Code>
       <Date>date2</Date>
       <Time>time1</Time>
     </Code>
  <statustype>
  <Manifest>
      <address>address2</address>
  </Manifest>
</subscriptionfile>

<SubscriptionEvents>

 

Want an output like this:

 

Array
(
[subscriptionEvents]=>
[details] => Array
        (
            [0] => Array
                (
                    Subscriptionfile=>
                    filename=>filename1
                     statustype=>
                    Code=>
                    Date=>date1
                    Time=>time1
                    Manifest=>
                    address=>address1
                    ) 
            [1] => Array
               (   
                    Subscriptionfile=>
                    filename=>filename2
                     statustype=>
                    Code=>
                    Date=>date2
                    Time=>time2
                    Manifest=>
                    address=>address2
               )
         )
)

 

EDITED BY akitchin:  use code tags, and remember - only you can prevent hard-to-read code.

Link to comment
Share on other sites

Have you tried simplexml ?

<?php
$str = '<SubscriptionEvents>
<subscriptionFile>
  <filename>filename1</filename>
  <statustype>
     <Code>
       <Date>date1</Date>
       <Time>time1</Time>
     </Code>
  </statustype>
  <Manifest>
      <address>address1</address>
  </Manifest>
</subscriptionFile>

<subscriptionFile>
  <filename>filename2</filename>
  <statustype>
     <Code>
       <Date>date2</Date>
       <Time>time1</Time>
     </Code>
  </statustype>
  <Manifest>
      <address>address2</address>
  </Manifest>
</subscriptionFile>

</SubscriptionEvents>
';

$xml = simplexml_load_string($str);

echo '<pre>', print_r($xml, true), '</pre>';
?>

Link to comment
Share on other sites

there ya goes... that'll turn any string/file from xml, to a multidimentsional array :-)

<?php
function xmltoarray(&$string){
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parse_into_struct($parser, $string, $vals, $index);
xml_parser_free($parser);
$mnary=array();
$ary=&$mnary;
foreach($vals as $r){
  $t=$r['tag'];
  if($r['type']=='open'){
   if(isset($ary[$t])){
    if (isset($ary[$t][0])) $ary[$t][]=array();
    else $ary[$t]=array($ary[$t], array());
    $cv=&$ary[$t][count($ary[$t])-1];
   }else $cv=&$ary[$t];
   if(isset($r['attributes'])){
    foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v;
   }
   $cv['_c']=array();
   $cv['_c']['_p']=&$ary;
   $ary=&$cv['_c'];
  }elseif($r['type']=='complete'){
   if(isset($ary[$t])){
    if (isset($ary[$t][0])) $ary[$t][]=array();
    else $ary[$t]=array($ary[$t], array());
    $cv=&$ary[$t][count($ary[$t])-1];
   }else $cv=&$ary[$t];
   if (isset($r['attributes'])){
    foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v;
   }
   $cv['_v']=(isset($r['value']) ? $r['value'] : '');
  }elseif($r['type']=='close'){
   $ary=&$ary['_p'];
  }
}
_del_p($mnary);
return $mnary;
}

function _del_p(&$ary){
foreach($ary as $k=>$v){
  if($k==='_p') unset($ary[$k]);
  elseif(is_array($ary[$k])) _del_p($ary[$k]);
}
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.