A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearParsing multiple XML feeds with PHP into one sorted array - page 1
User InfoPosts
Parsing multiple XML feeds with PHP into one sorted array#1
I want to create a sortable list that looks something like


$VAR1[0], $VAR2[0]...
$VAR1[1], $VAR2[1]...


The data comes from multiple same structured xml files:

$xmlfile="
<Level1>
<Level2>
<Level2Item VAR1="1" VAR2="2" ... />
<Level2Item VAR1="4" VAR2="5" ... />
<Level2Item VAR1="7" VAR2="8" ... />
</Level2>
</Level1>";

//Extract each item
$xml = new SimpleXMLElement($xmlfile);
foreach ($xml->Level2[0] as $result) {
array_push($VAR1Array, $result[(VAR1(]);
array_push($VAR2Array, $result[(VAR2(]);
//... etc etc
}
//sort
//$sortedArray = sort($VAR1Array);


Output

Array(
[0] => SimpleXMLElement Object([0] => 1)
[1] => SimpleXMLElement Object([0] => 4)
[2] => SimpleXMLElement Object([0] => 7)
)


From this XML structure, what(s the best way of storing the data in one array? I want to be able to gather all the data in one array so I can sort it by one or 2 VARs and display the results.

posted date: 2009-04-08 18:34:00


Re: Parsing multiple XML feeds with PHP into one sorted array#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-08 18:34:01


Re: Parsing multiple XML feeds with PHP into one sorted array#3
If I were you, I(d just push all of the SimpleXMLElements onto an array and then use uasort() with a custom callback function to sort as you desire. Does that work?

posted date: 2009-04-08 18:55:00


Re: Parsing multiple XML feeds with PHP into one sorted array#4
$xml = simplexml_load_file(...);$table = array();foreach ($xml->Level2[0] as $result) $table[] = $result->attributes();function cmp_row($a, $b, $sortColumn){ if ($a == $b) return 0; return ($a You can also keep the SimpleXMLElements and sort them directly, if you want, like apinstein said.

posted date: 2009-04-09 04:52:00


Re: Parsing multiple XML feeds with PHP into one sorted array#5
I have no idea why this isn't showing correctly. Preview shows it just fine.

posted date: 2009-04-09 04:55:00


Re: Parsing multiple XML feeds with PHP into one sorted array#6
I(m not quite sure what kind of sorting you(re trying to do (you should specify with some examples). But optimally, you wouldn(t be loading XML fragments into your array. $xmlfile="<Level1> <Level2> <Level2Item VAR1="1" VAR2="2" ... /> <Level2Item VAR1="4" VAR2="5" ... /> <Level2Item VAR1="7" VAR2="8" ... /> </Level2></Level1>";//Extract each item$xml = new SimpleXMLElement($xmlfile);foreach ($xml->Level2[0] as $result) { $VAR1Array[] = (int) $result[(VAR1(]; $VAR2Array[] = (int) $result[(VAR2(]; //... etc etc}Last, sort() works by reference, so don(t equate it to a variable (i.e. just say sort($array); as the entire line, and then $array will be sorted. If you cast as ints like I do in the above example you can use php(s default sort function without using a user-defined comparison function like others suggested. And array_push is a little bit slower and harder to read than using php(s $var[] syntax to add a new element to an array.

posted date: 2009-05-24 09:14:00


Re: Parsing multiple XML feeds with PHP into one sorted array#7
Also, I(m fully sure that you can(t assign $xmlfile that way (making unescaped double quotes inside double quotes.In this code, the best way to define $xmlfile would be:$xmlfile = <<<XML<Level1> <Level2> <Level2Item VAR1="1" VAR2="2" ... /> <Level2Item VAR1="4" VAR2="5" ... /> <Level2Item VAR1="7" VAR2="8" ... /> </Level2></Level1>XML;or$xmlfile = (<Level1> <Level2> <Level2Item VAR1="1" VAR2="2" ... /> <Level2Item VAR1="4" VAR2="5" ... /> <Level2Item VAR1="7" VAR2="8" ... /> </Level2></Level1>(;

posted date: 2009-07-08 15:41:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.