Create a multidimensional array from XML doc

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chuy08

    Create a multidimensional array from XML doc

    Currently I have the following XML document:

    <Programs>
    <Program>
    <ProductTypeNam e>30 Yr Fixed</ProductTypeName >
    <Rate>6.250</Rate>
    <APR>6.274</APR>
    <InvestorName>H elos</InvestorName>
    <Payment>2832.2 98</Payment>
    </Program>
    <Program>
    <ProductTypeNam e>30 Yr Fixed</ProductTypeName >
    <Rate>9.250</Rate>
    <APR>9.293</APR>
    <InvestorName>A valon</InvestorName>
    <Payment>3784.3 06</Payment>
    </Program>
    <Program>
    <ProductTypeNam e>5/1 Arm</ProductTypeName >
    <Rate>6.375</Rate>
    <APR>6.399</APR>
    <InvestorName>C arl1</InvestorName>
    <Payment>2869.8 02</Payment>
    </Program>
    </Programs>

    I load it up in a domdocument and iterate through it with the following:

    $returns = array();
    for($i = 0; $i < $xmlproducttype->length; $i++)
    {
    $returns[$i]['ProductTypeNam e'] =
    $xmlproducttype->item($i)->nodeValue;
    $returns[$i]['InvestorName'] =
    $xmlinvestornam e->item($i)->nodeValue;
    $returns[$i]['Rate'] = $xmlrate->item($i)->nodeValue;
    $returns[$i]['APR'] = $xmlapr->item($i)->nodeValue;
    $returns[$i]['Payment'] = $xmlpayment->item($i)->nodeValue;
    }

    Giving me the following style array:

    $returns['0']['ProductTypeNam e']

    What I need to figure out is how to iterate through this XML document to
    make an array where the unique ProductTypeName , which can be repeated many
    times, is the key name and the values are the other elements in the xml
    document. I am looking to get something like the following:

    $records=array(
    array('30 year fixed' =>
    array('Investor Name' =Carl1, 'Rate'=>6.0, ...)
    array('Investor Name' =Avalon, 'Rate'=>6.0, ...)

    print_r($record s['30 year fixed']['InvestorName']) = Avalon



    --
    Posted via a free Usenet account from http://www.teranews.com

  • petersprc

    #2
    Re: Create a multidimensiona l array from XML doc

    Hi,

    You could use the xml2array function at php.net/dom:



    An alternate way to do this might be to use SimpleXML:

    <?
    programs = simplexml_load_ string($yourXml );
    foreach ($programs->Program as $item) {
    $items[(string)$item->ProductTypeNam e][] = $item;
    }
    echo "Helos = {$items['30 Yr Fixed'][0]->InvestorName}\ n";
    echo "Carl1 = {$items['5/1 Arm'][0]->InvestorName}\ n";
    ?>

    HTH,

    John Peters

    On Feb 16, 4:02 pm, "Chuy08" <chu...@yahoo.c omwrote:
    Currently I have the following XML document:
    >
    <Programs>
    <Program>
    <ProductTypeNam e>30 Yr Fixed</ProductTypeName >
    <Rate>6.250</Rate>
    <APR>6.274</APR>
    <InvestorName>H elos</InvestorName>
    <Payment>2832.2 98</Payment>
    </Program>
    <Program>
    <ProductTypeNam e>30 Yr Fixed</ProductTypeName >
    <Rate>9.250</Rate>
    <APR>9.293</APR>
    <InvestorName>A valon</InvestorName>
    <Payment>3784.3 06</Payment>
    </Program>
    <Program>
    <ProductTypeNam e>5/1 Arm</ProductTypeName >
    <Rate>6.375</Rate>
    <APR>6.399</APR>
    <InvestorName>C arl1</InvestorName>
    <Payment>2869.8 02</Payment>
    </Program>
    </Programs>
    >
    I load it up in a domdocument and iterate through it with the following:
    >
    $returns = array();
    for($i = 0; $i < $xmlproducttype->length; $i++)
    {
    $returns[$i]['ProductTypeNam e'] =
    $xmlproducttype->item($i)->nodeValue;
    $returns[$i]['InvestorName'] =
    $xmlinvestornam e->item($i)->nodeValue;
    $returns[$i]['Rate'] = $xmlrate->item($i)->nodeValue;
    $returns[$i]['APR'] = $xmlapr->item($i)->nodeValue;
    $returns[$i]['Payment'] = $xmlpayment->item($i)->nodeValue;
    >
    }
    >
    Giving me the following style array:
    >
    $returns['0']['ProductTypeNam e']
    >
    What I need to figure out is how to iterate through this XML document to
    make an array where the unique ProductTypeName , which can be repeated many
    times, is the key name and the values are the other elements in the xml
    document. I am looking to get something like the following:
    >
    $records=array(
    array('30 year fixed' =>
    array('Investor Name' =Carl1, 'Rate'=>6.0, ...)
    array('Investor Name' =Avalon, 'Rate'=>6.0, ...)
    >
    print_r($record s['30 year fixed']['InvestorName']) = Avalon
    >
    --
    Posted via a free Usenet account fromhttp://www.teranews.co m

    Comment

    Working...