By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,614 Members | 1,674 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,614 IT Pros & Developers. It's quick & easy.

Create a multidimensional array from XML doc

P: n/a
Currently I have the following XML document:

<Programs>
<Program>
<ProductTypeName>30 Yr Fixed</ProductTypeName>
<Rate>6.250</Rate>
<APR>6.274</APR>
<InvestorName>Helos</InvestorName>
<Payment>2832.298</Payment>
</Program>
<Program>
<ProductTypeName>30 Yr Fixed</ProductTypeName>
<Rate>9.250</Rate>
<APR>9.293</APR>
<InvestorName>Avalon</InvestorName>
<Payment>3784.306</Payment>
</Program>
<Program>
<ProductTypeName>5/1 Arm</ProductTypeName>
<Rate>6.375</Rate>
<APR>6.399</APR>
<InvestorName>Carl1</InvestorName>
<Payment>2869.802</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]['ProductTypeName'] =
$xmlproducttype->item($i)->nodeValue;
$returns[$i]['InvestorName'] =
$xmlinvestorname->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']['ProductTypeName']

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('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 http://www.teranews.com

Feb 16 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi,

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

http://us2.php.net/manual/en/ref.dom.php#68967

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";
?>

HTH,

John Peters

On Feb 16, 4:02 pm, "Chuy08" <chu...@yahoo.comwrote:
Currently I have the following XML document:

<Programs>
<Program>
<ProductTypeName>30 Yr Fixed</ProductTypeName>
<Rate>6.250</Rate>
<APR>6.274</APR>
<InvestorName>Helos</InvestorName>
<Payment>2832.298</Payment>
</Program>
<Program>
<ProductTypeName>30 Yr Fixed</ProductTypeName>
<Rate>9.250</Rate>
<APR>9.293</APR>
<InvestorName>Avalon</InvestorName>
<Payment>3784.306</Payment>
</Program>
<Program>
<ProductTypeName>5/1 Arm</ProductTypeName>
<Rate>6.375</Rate>
<APR>6.399</APR>
<InvestorName>Carl1</InvestorName>
<Payment>2869.802</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]['ProductTypeName'] =
$xmlproducttype->item($i)->nodeValue;
$returns[$i]['InvestorName'] =
$xmlinvestorname->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']['ProductTypeName']

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('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 fromhttp://www.teranews.com

Feb 17 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.