Create a multidimensional array from XML doc

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Currently I have the following XML document:

        <ProductTypeName>30 Yr Fixed</ProductTypeName>
        <ProductTypeName>30 Yr Fixed</ProductTypeName>
    <ProductTypeName>5/1 Arm</ProductTypeName>

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]['ProductTypeName'] =  
        $returns[$i]['InvestorName'] =  
        $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:


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:

    array('30 year fixed' =>
        array('InvestorName' => Carl1, 'Rate'=>6.0, ...)
        array('InvestorName' => Avalon, 'Rate'=>6.0, ...)

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

Posted via a free Usenet account from

Re: Create a multidimensional array from XML doc


You could use the xml2array function at

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

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


John Peters

Quoted text here. Click to load it

Site Timeline