You are here: Re: Create a multidimensional array from XML doc « PHP Programming Language « IT news, forums, messages
Re: Create a multidimensional array from XML doc

Posted by petersprc on 02/17/07 19:42

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.com> wrote:
> 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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация