You are here: Re: Need help sorting a XML file « PHP Programming Language « IT news, forums, messages
Re: Need help sorting a XML file

Posted by BJMurphy on 08/17/07 15:24

On Aug 17, 11:12 am, "Brian" <br...@aol.com> wrote:
> I have a similar xml file and I redisplay it with the following array in
> PHP. What I would like to do is sort the <id> field before it is
> redisplayed. Looking for a sample code.
>
> Thanks
>
> <rss>
> <item>
> <id>3</id>
> <name>Brian</name>
> </item>
> <item>
> <id>1</id>
> <name>James</name>
> </item>
> <item>
> <id>5</id>
> <name>Mark</name>
> </item>
> <item>
> <id>2</id>
> <name>Keith</name>
> </item>
> <item>
> <id>4</id>
> <name>Derek</name>
> </item>
> </rss>
>
> <?php
> $url = "test.xml";
> $data = implode("", file($url));
> preg_match_all ("/<item>([^`]*?)<\/item>/", $data, $matches);
>
> // Begin feed
> header ("Content-Type: text/xml; charset=ISO-8859-1");
> echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
> ?>
>
> <rss>
>
> <?
>
> foreach ($matches[0] as $match) {
>
> preg_match ("/<id>([^`]*?)<\/id>/", $match, $temp);
> $id = $temp['1'];
>
> preg_match ("/<name>([^`]*?)<\/name>/", $match, $temp);
> $description = $temp['1'];
>
> // Echo RSS XML
> echo "<item>\n";
> echo "\t\t\t<id>" . $id . "</id>\n";
> echo "\t\t\t<name>" . $name . "</name>\n";
> echo "\t\t</item>\n";
>
> }
>
> ?></rss>

Well, I think you mean to use $description in that second to last echo
statement, but that's not your question. If your ID's are unique, why
not build an array that maps the ID to the data (in this case just a
single string) and then you can sort and display as you please?

 

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

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